来自视图模型的条件绑定

本文关键字:条件 绑定 模型 视图 | 更新日期: 2023-09-27 18:36:59

我有几个列表视图,有两个像
。我正在为另一个应用程序构建一个统计应用程序。
每行应有两列。(名称,总和)或(名称,计数)
问题是模型有三个(名称,总和,计数)。并且我想有一个通用开关来确定应该在所有 6 个列表视图中显示哪一列。有什么解决方案吗?

来自视图模型的条件绑定

您可以对 ListView 使用样式,并且仅根据显示所追求的视图的属性添加所需的列。

CountSumSwitch 是我在 MainWindow 中拥有的布尔依赖属性,您可以将其切换为 true 以显示所有列表视图中的计数,将其切换为 false 以显示所有列表视图中的总和。

     <Style TargetType="{x:Type ListView}">
        <Style.Triggers>
            <!-- This binding needs to point to some global propery that you'll change to switch views.-->
            <DataTrigger Binding="{Binding CountSumSwitch}" Value="True">
                <Setter Property="View">
                    <Setter.Value>
                        <GridView>
                            <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
                            <GridViewColumn Width="140" Header="Count" DisplayMemberBinding="{Binding Count}" />
                        </GridView>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding CountSumSwitch}" Value="False">
                <Setter Property="View">
                    <Setter.Value>
                        <GridView>
                            <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
                            <GridViewColumn Width="140" Header="Sum" DisplayMemberBinding="{Binding Sum}" />
                        </GridView>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

编辑:

为了实际显示这一点,我将添加一个示例,它显示绑定到集合的列表视图,其中包含用于切换所用列的复选框。创建一个新的 WPF 应用程序,将 MainWindow 类替换为

public partial class MainWindow : Window
    {
        public bool CountSumSwitch
        {
            get { return (bool)GetValue(CountSumSwitchProperty); }
            set { SetValue(CountSumSwitchProperty, value); }
        }
        public static readonly DependencyProperty CountSumSwitchProperty = DependencyProperty.Register("CountSumSwitch", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));

        public List<TheItem> ITems
        {
            get { return (List<TheItem>)GetValue(ITemsProperty); }
            set { SetValue(ITemsProperty, value); }
        }
        public static readonly DependencyProperty ITemsProperty = DependencyProperty.Register("ITems", typeof(List<TheItem>), typeof(MainWindow), new UIPropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();
            Random rnd = new Random();
            ITems = new List<TheItem>(new TheItem[]
            {
                new TheItem () { Name = "Item 1", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 2", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 3", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 4", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 5", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 6", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 7", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 8", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
                new TheItem () { Name = "Item 9", Count = rnd.Next(100), Sum = rnd.Next (100)}, 
            });
            CountSumSwitch = false;
        }
        public class TheItem
        {
            public string Name { get; set; }
            public int Count { get; set; }
            public int Sum { get; set; }
        }
    }

并将此代码放在 MainWindow.xaml 中

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type ListView}">
            <Style.Triggers>
                <!-- This binding needs to point to some global propery that you'll change to switch views.-->
                <DataTrigger Binding="{Binding CountSumSwitch}" Value="True">
                    <Setter Property="View">
                        <Setter.Value>
                            <GridView>
                                <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
                                <GridViewColumn Width="140" Header="Count" DisplayMemberBinding="{Binding Count}" />
                            </GridView>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding CountSumSwitch}" Value="False">
                    <Setter Property="View">
                        <Setter.Value>
                            <GridView>
                                <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}" />
                                <GridViewColumn Width="140" Header="Sum" DisplayMemberBinding="{Binding Sum}" />
                            </GridView>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <CheckBox IsChecked="{Binding CountSumSwitch}"/>
        <ListView ItemsSource="{Binding ITems}" Margin="0,83,0,0"/>
    </Grid>
</Window>

为列表视图实现不同的项目模板,并根据您的目的切换它们。