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