简体   繁体   中英

WPF binding user-control in DataTemplate

I am struggling to bind my user-defined tooltip to the items of a 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).

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

Each user control should not set its DataContext property. This should be inherited, What you do inside each user control, is to bind each element to its UserControl ancestor. So, inside MyDocTooltip.xaml each element will be defined like this (bound to NameField property):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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