繁体   English   中英

WPF MVVM TreeView IsSelected绑定不起作用

[英]WPF MVVM TreeView IsSelected binding not working

我有一个问题,我看不清为什么它不起作用。 我有一个带有转换器的3层树状视图,它使我可以将两个第一层倒置。 在此TreeView上,我尝试使用IsSelected属性,但是由于某种原因,我不知道在ViewModel中永远不会调用它。

这是xaml。

<TreeView  Margin="0,0,0,0" Padding="0,7,0,7" BorderBrush="{x:Null}"  Background="#FFF8FAFB" Name="BusinessCaseTreeView">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemsSource>
        <MultiBinding Converter="{converter:BusinessCasesConverter}">
            <Binding Path="BusinessCases"></Binding>
            <Binding Path="GroupByCreditor"></Binding>
        </MultiBinding>
    </TreeView.ItemsSource>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type converter:BusinessCaseTreeViewableFirst}" ItemsSource="{Binding SecondLevel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FirstLevel.Id}" Margin="10,0,0,0" Foreground="#FF03577A" FontWeight="Bold" />
                <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF03577A" FontWeight="Bold"/>
                <TextBlock Text="{Binding FirstLevel.FirstName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF03577A" />
                <TextBlock Text="{Binding FirstLevel.LastName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF03577A" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type converter:BusinessCaseTreeViewModelSecond}" ItemsSource="{Binding  BusinessCases}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding SecondLevel.Id}" Margin="10,0,0,0" Foreground="#FF095A7C" FontWeight="Bold" />
                <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF095A7C" FontWeight="Bold"/>
                <TextBlock Text="{Binding SecondLevel.FirstName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF095A7C" />
                <TextBlock Text="{Binding SecondLevel.LastName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF095A7C" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type business:BusinessCase}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="(" Margin="10,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold" />
                <TextBlock Text="{Binding Id}" Margin="1,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold" />
                <TextBlock Text=")" Margin="2,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold"/>
                <TextBlock Text="{Binding CreationDate, StringFormat='{}{0:dd MMMM yyyy}'}" Margin="5,0,0,0" Foreground="#FF0B89BD" />
                <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold"/>
                <TextBlock Text="{Binding ClosureDate, StringFormat={}{0:dd MMMM yyyy}}" Margin="5,0,0,0" Foreground="#FF0B89BD" />
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

我从未到达ViewModel中的IsSelected setter。 如果您有任何想法,欢迎您。

提前致谢

它可能与您在ItemContainerStyle 另外,请确保IsSelected在所有添加到树视图的类型中

尝试这个:

<TreeView>
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected"
                    Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </TreeView.Resources>
    .....
</TreeView>

暂无
暂无

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

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