【问题标题】:WPF MVVM TreeView IsSelected binding not workingWPF MVVM TreeView IsSelected 绑定不起作用
【发布时间】:2014-03-03 20:57:56
【问题描述】:

我有一个问题,我看不出它为什么不起作用。 我有一个带有转换器的 3 级 Treeview,它允许我反转两个第一级。 在这个 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 设置器。如果您有任何想法,欢迎。

提前致谢

【问题讨论】:

  • 您的输出窗口中是否有任何绑定错误?

标签: c# wpf xaml mvvm treeview


【解决方案1】:

这可能与您在ItemContainerStyle 中执行此操作有关。另外,请确保 IsSelected 在添加到树视图的所有类型中

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多