【发布时间】:2017-01-25 11:58:50
【问题描述】:
我在将上下文菜单绑定到文本框的附加属性时遇到问题。所以我有 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>
我的附加财产:
#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
谢谢
【问题讨论】:
-
这里加一些代码,方便理解
-
看看这个答案:stackoverflow.com/questions/3878620/…。上下文菜单中的 PlacementTarget 属性是您想要的?
-
感谢@Jamaxack,但它没有帮助。我正在尝试绑定到所有者附加属性!
-
能否请您显示您在
TextBox上设置所述附加属性的代码部分?
标签: wpf xaml binding contextmenu attached-properties