视图模型的条件绑定?

时间:2012-06-12 15:33:41

标签: c# wpf

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

2 个答案:

答案 0 :(得分:1)

您可以使用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>

答案 1 :(得分:1)

为列表视图实施不同的ItemTemplate并根据您的目的切换它们。