2012-06-12 96 views
0

我有幾個listviews像兩個columns。
我正在爲另一個應用程序構建一個統計應用程序。
每行應該有兩列。 (名稱,點心)或(姓名,計數)
的問題是,該模型具有三個(名稱,點心,計數)。而我想有一個普通開關來確定哪一列應當在所有6名列表視圖顯示。有沒有解決方案?從視圖模型的條件綁定?

+0

不知道這值得一downvote – Andy

回答

1

你可以使用樣式的ListView和只添加要取決於顯示了查看你後一個屬性欄。

CountSumSwitch的是,我在主窗口的布爾依賴屬性,你切換到真正的在所有的列表視圖,顯示計數和假所有的列表視圖中顯示的總和。

 <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應用程序,這個

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類,並把這個代碼在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> 
+0

這就是我一直在尋找,但一點修飾.. DataTrigger的關鍵是我..謝謝,我會努力 – GorillaApe