简体   繁体   English

WPF 在 DataTemplate 中绑定用户控件

[英]WPF binding user-control in DataTemplate

I am struggling to bind my user-defined tooltip to the items of a TreeView .我正在努力将我的用户定义工具提示绑定到TreeView的项目。 This is the XAML inside the TreeView : as you can see I bind my HierarchicalDataTemplate to the GroupWrapper (simple Class that exposes the properties Name and Children) and the DataTemplate to the DocWrapper (again, simple Class that has properties such as Name, Icon, BsonContent). This is the XAML inside the TreeView : as you can see I bind my HierarchicalDataTemplate to the GroupWrapper (simple Class that exposes the properties Name and Children) and the DataTemplate to the DocWrapper (again, simple Class that has properties such as Name, Icon, Bson 内容)。

<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>

It all works great.这一切都很好。 The Tree is automatically populated with the correct text and icons for all nodes BUT... my user-defined tooltip doesn't get the memo.树会自动填充所有节点的正确文本和图标但是......我的用户定义的工具提示没有得到备忘录。 The tooltip shows , but the name field is not populated.工具提示显示,但未填充名称字段。 Note that if I replace请注意,如果我更换

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

with:和:

<local:MyDocTooltip   
    NameField="TESTSTRING"/>

The test string is correctly displayed in the Tooltip.测试字符串正确显示在工具提示中。

This is what my user-defined tooltip looks like:这是我的用户定义工具提示的样子:

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); }
        }
    }
}

I guess something to do with my tooltip's data context being set to its own ViewModel?我猜想与我的工具提示的数据上下文设置为它自己的 ViewModel 有关吗? I've been trying with relative references but with no luck.我一直在尝试相对参考,但没有运气。 Any help appreciated.任何帮助表示赞赏。 Cheers.干杯。

OK my whole setup was wrong.好的,我的整个设置都是错误的。 (Thanks @ASh) For those out there who are struggling with similar issues: (感谢@ASh)对于那些正在努力解决类似问题的人:

Each user control should not set its DataContext property.每个用户控件都不应设置其DataContext属性。 This should be inherited, What you do inside each user control, is to bind each element to its UserControl ancestor.这应该是继承的,您在每个用户控件中所做的是将每个元素绑定到其UserControl祖先。 So, inside MyDocTooltip.xaml each element will be defined like this (bound to NameField property):因此,在MyDocTooltip.xaml ,每个元素都将像这样定义(绑定到NameField属性):

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

Then I do the same in the TreeView (which is also inside a user control):然后我在 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>

And it all seems to work fine.这一切似乎都很好。 Please do let me know if I'm still doing it wrong.如果我仍然做错了,请告诉我。 Thanks for the help.谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM