根据绑定项目 - 我使用不同的数据模板来显示数据。现在我还需要修改树视图本身的行为/样式。是否可以根据对象属性切换项目的样式?现在它只有一个指定:ItemContainerStyle="{StaticResource TreeViewItemStyleFolder}"
我想创造第二种风格TreeViewItemStyleDocument
当前 XAML(带有自定义样式和模板选择器)
<sdk:TreeView ItemsSource="{Binding Items}"
Grid.Row="1"
Style="{StaticResource TreeViewStyle1}"
ItemContainerStyle="{StaticResource TreeViewItemStyleFolder}"
>
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
<DocumentManagement:DocumentTreeViewItemTemplateSelector
Content="{Binding}">
<DocumentManagement:DocumentTreeViewItemTemplateSelector.FolderTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- FOLDER ICON AND CAPTION -->
<Image Source="{Binding IconSource}" Width="24" Height="24" />
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left"
Grid.Column="1" Margin="5,0"
Text="{Binding Folder.FolderId}" FontSize="12" Foreground="#2C2C2C" />
</Grid>
</DataTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector.FolderTemplate>
<DocumentManagement:DocumentTreeViewItemTemplateSelector.DocumentTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<TextBlock FontSize="10" Foreground="#2C2C2C">
<Run Text="Added by" />
<Run Text="{Binding Document.MEMUser.UserName}" />
<Run Text=" on " />
<Run Text="{Binding CreatedOn, Converter={StaticResource DateTimeToStringConverter}}" />
</TextBlock>
<!--BIND COMMANDS TO PARENT ViewModel to process operations-->
<Button Content="Delete" Command="{Binding DataContext.DeleteCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
<Button Content="Download" Command="{Binding DataContext.DownloadCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
</StackPanel>
</Grid>
</DataTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector.DocumentTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector>
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>
编辑:
添加了基于属性切换 ItemContainerStyle 的触发器,但我认为问题在于我使用的是分层数据模板。如果我在属性上放置断点IsFolder
- 没有源对象。
<sdk:TreeView x:Name="DocumentsTreeView" ItemsSource="{Binding Items}"
Grid.Row="1"
Style="{StaticResource TreeViewStyleTransparent}">
<!-- ItemContainerStyle="{StaticResource TreeViewItemStyleFolders}"-->
<i:Interaction.Triggers>
<ei:DataTrigger Value="False" Binding="{Binding IsFolder}">
<ei:ChangePropertyAction TargetName="DocumentTreeView" PropertyName="ItemContainerStyle"
Value="{StaticResource TreeViewItemStyleFolders}" />
</ei:DataTrigger>
<ei:DataTrigger Value="True" Binding="{Binding IsFolder}">
<ei:ChangePropertyAction TargetName="DocumentTreeView" PropertyName="ItemContainerStyle"
Value="{StaticResource TreeViewItemStyleDocuments}" />
</ei:DataTrigger>
</i:Interaction.Triggers>
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
<DocumentManagement:DocumentTreeViewItemTemplateSelector
Content="{Binding}">
<DocumentManagement:DocumentTreeViewItemTemplateSelector.FolderTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- FOLDER ICON AND CAPTION -->
<Image Source="{Binding IconSource}" Width="24" Height="24" />
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left"
Grid.Column="1" Margin="5,0"
Text="{Binding Folder.FolderId}" FontSize="12" Foreground="#2C2C2C" />
</Grid>
</DataTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector.FolderTemplate>
<DocumentManagement:DocumentTreeViewItemTemplateSelector.DocumentTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<TextBlock FontSize="10" Foreground="#2C2C2C">
<Run Text="Added by" />
<Run Text="{Binding Document.MEMUser.UserName}" />
<Run Text="on" />
<Run Text="{Binding Document.CreatedOn, Converter={StaticResource DateTimeToStringConverter}}" />
</TextBlock>
<!--BIND COMMANDS TO PARENT ViewModel to process operations-->
<Button Content="Delete" Command="{Binding DataContext.DeleteCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
<Button Content="Download" Command="{Binding DataContext.DownloadCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
</StackPanel>
</Grid>
</DataTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector.DocumentTemplate>
</DocumentManagement:DocumentTreeViewItemTemplateSelector>
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>