繁体   English   中英

WPF 在 DataTemplate 中绑定用户控件

[英]WPF binding user-control in DataTemplate

我正在努力将我的用户定义工具提示绑定到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, 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>

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

<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 有关吗? 我一直在尝试相对参考,但没有运气。 任何帮助表示赞赏。 干杯。

好的,我的整个设置都是错误的。 (感谢@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>

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

暂无
暂无

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

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