MVVM WPF如何基于来自xaml的DataContext的DataType在TreeViewItem上设置样式

时间:2013-11-18 18:00:08

标签: wpf xaml mvvm

即使某些treeviewitems无法扩展?

所以我有一个使用这些资源的树视图,但是一个项类型的viewmodel是分层的,而另一个项无法展开。我觉得我不应该在expandpandl项的viewmodel中创建一个IsExpanded属性,但我不知道如何根据内容的数据类型为项类型设置样式。如果从xaml无法做到这一点,那么可能在代码背后,但在这种情况下,我可能只是将IsExpanded属性添加到不可扩展的项目并将其保留为false

<DaedalusGraphViewer:MultiSelectTreeView.Resources>
              <Style TargetType="{x:Type DaedalusGraphViewer:MultiSelectTreeViewItem}" BasedOn="{StaticResource NameTreeViewBusItemStyle}" />
              <HierarchicalDataTemplate 
                ItemsSource ="{Binding Path = bits}"
                DataType="{x:Type ViewModels:BusViewModel}" 
                >
                <Grid>
                  <Components:CenteredTextBlock
                    BorderThickness="{Binding RelativeSource ={RelativeSource AncestorType={x:Type DaedalusGraphViewer:MultiSelectTreeView}}, Path=BorderThickness}"
                    BorderBrush="{Binding RelativeSource ={RelativeSource AncestorType={x:Type DaedalusGraphViewer:MultiSelectTreeView}}, Path=BorderBrush}"
                    HorizontalAlignment="Stretch"
                    Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
                    MainText="{Binding Path = Alias}" 
                    />
                </Grid>
              </HierarchicalDataTemplate>
              <DataTemplate
                DataType="{x:Type ViewModels:BitViewModel}"
                >
                <Grid>
                  <Components:CenteredTextBlock
                    BorderThickness="{Binding RelativeSource ={RelativeSource AncestorType={x:Type DaedalusGraphViewer:MultiSelectTreeView}}, Path=BorderThickness}"
                    BorderBrush="{Binding RelativeSource ={RelativeSource AncestorType={x:Type DaedalusGraphViewer:MultiSelectTreeView}}, Path=BorderBrush}"
                    HorizontalAlignment="Stretch"
                    Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
                    MainText="{Binding Path = Alias}"
                    />
                </Grid>
              </DataTemplate>
            </DaedalusGraphViewer:MultiSelectTreeView.Resources>

0 个答案:

没有答案