【问题标题】:Binding ContextMenu property to owner property将 ContextMenu 属性绑定到所有者属性
【发布时间】: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


【解决方案1】:

这是 ContextMenu 的一个普遍问题,因为它并不总是具有假定父级的数据上下文。更多信息请阅读继承上下文https://blogs.msdn.microsoft.com/nickkramer/2006/08/17/whats-an-inheritance-context/

正如上述博客文章的 cmets 所述,您可能需要将上下文菜单的数据上下文显式设置为文本框。

【讨论】:

    【解决方案2】:

    坏消息: CollectionContainer 没有 DataContext。在绑定中,您只能使用 Source={x:reference XXX}。 XXX 必须在您的样式/资源字典之外进行初始化。

    你可以做什么:

    要将具有 DataContext 的内容绑定到 TextBox 的附加属性是: 将 TextBox 设置为 ContextMenu 的 DataContext。 因此,您可以使用 PlacementTarget 属性,因为您的上下文菜单挂在您的 TextBox 上。进一步你可以正常绑定。

    <ContextMenu x:Name="uiContexMenu" DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
    
    <ComboBox ItemsSource="{Binding Path=(local:Extensions.ExtendCommands)}"/>
    ...
    </ContextMenu>
    

    当然你必须设置你的附加属性:

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    <TextBox Text="***" Style="{StaticResource DefaultTextBox}">
                    <local:Extensions.ExtendCommands>
                        <x:Array Type="{x:Type sys:String}">
                            <sys:String>Text 1</sys:String>
                            <sys:String>Text 2</sys:String>
                            <sys:String>Text 3</sys:String>
                        </x:Array>
                    </local:Extensions.ExtendCommands>
                </TextBox>
    

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 2017-01-27
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      相关资源
      最近更新 更多