我有几个带有两个列的列表视图。
我正在为另一个应用程序构建一个统计应用程序。
每行应该有两列。(名称,总和)或(名称,计数)
问题是模型有三个(名称,总和,计数)。我想要一个通用开关来确定应该在所有 6 个列表视图中显示哪一列。有什么解决办法吗?
问问题
634 次
2 回答
1
为您的列表视图实现不同的ItemTemplate并根据您的目的切换它们。
于 2012-06-12T16:08:20.327 回答
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>
于 2012-06-12T15:42:26.300 回答