Qut*_*lov 5 wpf xaml binding contextmenu attached-properties
我将上下文菜单绑定到文本框的附加属性时遇到问题.所以我有TextBox,右键单击它有一个上下文菜单.那么如何将上下文菜单的属性绑定到WPF XAML中TextBox的附加属性?在这里,我尝试绑定到TextBox但它没有帮助
<Style x:Key="DefaultTextBox" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="{DynamicResource ThemeSecondary}"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu x:Name="uiContexMenu">
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem Command="Cut" Header="Cut">
<MenuItem.Icon>
<Viewbox Width="16" Height="16">
<TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
</Viewbox>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Copy" Header="Copy">
<MenuItem.Icon>
<Viewbox Width="16" Height="16">
<TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
</Viewbox>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Paste" Header="Paste">
<MenuItem.Icon>
<Viewbox Width="16" Height="16">
<TextBlock FontFamily="{DynamicResource IconFont}" Text="?"/>
</Viewbox>
</MenuItem.Icon>
</MenuItem>
<CollectionContainer Collection="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}, Path=Extensions.ExtendCommands}"/>
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)
我的附属物:
#region ExtendCommands dependency property
public static IEnumerable GetExtendCommands(DependencyObject obj)
{
return (IEnumerable)obj.GetValue(ExtendCommandsProperty);
}
public static void SetExtendCommands(DependencyObject obj, IEnumerable value)
{
obj.SetValue(ExtendCommandsProperty, value);
}
// Using a DependencyProperty as the backing store for ExtendCommands. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ExtendCommandsProperty =
DependencyProperty.RegisterAttached("ExtendCommands", typeof(IEnumerable), typeof(Extensions), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
#endregion
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 0
这是 ContextMenu 的一个普遍问题,因为它并不总是具有假定父级的数据上下文。有关继承上下文的更多信息,请阅读https://blogs.msdn.microsoft.com/nickkramer/2006/08/17/whats-an-inheritance-context/
\n\n正如该博客文章的评论中所提到的,您可能需要将上下文菜单的数据上下文显式设置为文本框。
\n| 归档时间: |
|
| 查看次数: |
573 次 |
| 最近记录: |