Reputation: 3641
I have several listviews with two collumns like.
I am building a stats application for another application.
Each row should have two columns.
(Name,Sum) or (Name,Count)
The problem is that the model has three(Name,Sum,Count).And i want to have a general switch to determine which column should be shown at all 6 listviews. Is there any solution for this?
Upvotes: 0
Views: 717
Reputation: 5421
Implement different ItemTemplates for your listviews and switch them according to your purposes.
Upvotes: 1
Reputation: 6466
You could use a style for the ListView and only add the columns that you want depending on a property that shows which view you're after.
CountSumSwitch is a boolean dependency property that I have in MainWindow, you switch it to true to display the counts in all listviews and false to display the sum in all listviews.
<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>
Edit:
To show this in action I'll add an example, it shows a listview bound to a collection with a checkbox to toggle the columns that are used. Create a new WPF application, replace the MainWindow class with this
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; }
}
}
And put this code in the 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>
Upvotes: 1