0

我正在努力将我的用户定义工具提示绑定到TreeView. 这是内部的 XAML TreeView:如您所见,我将 my 绑定HierarchicalDataTemplateGroupWrapper(公开属性 Name 和 Children 的简单类)和DataTemplateDocWrapper同样,具有诸如 Name、Icon、BsonContent 等属性的简单类)。

<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type tmistruct:GroupWrapper}" ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type tmistruct:DocWrapper}" >
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding Icon}"/>
        <TextBlock Text="{Binding Name}" Tag="{Binding BsonContent}" VerticalAlignment="Center" Style="{StaticResource TreeViewTextboxItemStyle}"/>
        <StackPanel.ToolTip>
            <local:MyDocTooltip   
                NameField="{Binding Name}"/>
        </StackPanel.ToolTip>
    </StackPanel>
</DataTemplate>
</TreeView.Resources>

这一切都很好!树会自动填充所有节点的正确文本和图标但是......我的用户定义的工具提示没有得到备忘录。工具提示显示,但未填充名称字段。请注意,如果我更换

<local:MyDocTooltip
    NameField="{Binding Name}"/>

和:

<local:MyDocTooltip   
    NameField="TESTSTRING"/>

测试字符串正确显示在工具提示中。

这是我的用户定义工具提示的样子:

namespace MyControls
{
    /// <summary>
    /// Interaction logic for MyDocTooltip.xaml
    /// </summary>
    public partial class MyDocTooltip : UserControl
    {
        public MyDocTooltip()
        {
            InitializeComponent();
            DataContext = this;
        }

        public static readonly DependencyProperty NameFieldProperty = DependencyProperty.Register("NameField", typeof(string), typeof(MyDocTooltip));
        public string NameField
        {
            get { return (string)GetValue(NameFieldProperty); }
            set { SetValue(NameFieldProperty, value); }
        }
    }
}

我猜想与我的工具提示的数据上下文被设置为它自己的 ViewModel 有关吗?我一直在尝试相对参考,但没有运气。任何帮助表示赞赏!干杯。

4

1 回答 1

0

好的,我的整个设置都是错误的。(感谢@ASh)对于那些正在努力解决类似问题的人:

每个用户控件都不应该设置它的DataContext属性!这应该被继承。您在每个用户控件中所做的是将每个元素绑定到其UserControl祖先。因此,在MyDocTooltip.xaml每个元素内部都将像这样定义(绑定到NameField属性):

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=NameField}"/>

然后我在 TreeView 中做同样的事情(它也在用户控件中):

    <TreeView x:Name="treeViewCollection" Grid.Row="2" Grid.ColumnSpan="2"  ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=FilteredGroups, UpdateSourceTrigger=PropertyChanged}" >
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type tmistruct:GroupWrapper}" ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
            <DataTemplate x:Name="TreeViewDataTemplate"  DataType="{x:Type tmistruct:DocWrapper}" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Icon}"/>
                    <TextBlock Text="{Binding Name}" Tag="{Binding BsonContent}" VerticalAlignment="Center"/>
                    <StackPanel.ToolTip>
                        <local:MyDocTooltip                             
                            NameField="{Binding Name}"
                            />
                    </StackPanel.ToolTip>                        
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

这一切似乎都很好。如果我仍然做错了,请告诉我。谢谢您的帮助。

于 2020-04-03T13:40:46.607 回答