簡體   English   中英

通過綁定到不同的TreeView selectedItem使用不同的數據模板

[英]Use different datatemplate by binding to different TreeView selectedItem

我建立了一個WPF MVVM TreeView,它顯示了不同的元素。

BaseElement
-CatA
-SubItemA
-CatB
-SubItemB

基於類,我想使用其他數據模板。 對於每種類型。

到目前為止,我可以連接到選定的項目,但是我不確定如何管理不同的數據模板。

public class SubItem
{
    public string Type { get; set; }
    public string Name { get; set; }
}


    <StackPanel Grid.Column="2" DataContext="{Binding ElementName=myTreeView, Path=SelectedItem}">
        <TextBox Text="{Binding Parent.Name}" />
        <TextBox Text="{Binding Path=Name, Mode=TwoWay}" />
    </StackPanel>

[11月15日更新]

           <HierarchicalDataTemplate x:Key="L3Template" ItemsSource="{Binding L4Collection}" ItemTemplate="{StaticResource L4Template}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="CategoryTemplate" ItemsSource="{Binding L3Collection}" ItemTemplate="{StaticResource L3Template}">
                <StackPanel>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="L1Template" ItemsSource="{Binding CategoryCollection}" ItemTemplate="{StaticResource CategoryTemplate}">
                <StackPanel>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

[/ 11月15日更新]

如果子項是不同的類,那么這很簡單:將用於每個類的數據模板添加到資源部分。 如果子項目需要基於枚舉屬性值的不同模板,那么您將需要一個datatemplateselector。 這有點麻煩。

假設您已將類L1Class,L3Class命名為Category,並且本地指向這些類的名稱空間:

    <TreeView ...>
        <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:L3Class}" ItemsSource="{Binding L4Collection}" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type local:Category}" ItemsSource="{Binding L3Collection}" >
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type local:L1Class}" ItemsSource="{Binding CategoryCollection}" >
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
    </TreeView>

請注意在資源部分中使用了隱式數據模板(沒有鍵,只有數據類型!)。

以防萬一它會幫助別人:

        <ContentPresenter Grid.Column="2" Content="{Binding ElementName=myTreeView, Path=SelectedItem}">
            <ContentPresenter.Resources>
                <DataTemplate DataType="{x:Type local:L1ViewModel}">
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate DataType="{x:Type local:CategoryViewModel}">
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>

                <DataTemplate DataType="{x:Type local:L3ViewModel}">
                    <StackPanel>
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>

                <DataTemplate DataType="{x:Type local:L4ViewModel}">
                    <StackPanel>
                         <TextBox Text="{Binding Parent.Name}" />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ContentPresenter.Resources>
          </ContentPresenter>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM