2012-11-21 85 views
1

我有一個DataGrid的一些列定義。行被綁定到ObservableCollection。 網格旁邊有一個按鈕,該按鈕應該是可見或不可見的,具體取決於行數。當有2個(或更多)行時,它應該是可見的。 這個想法是使用DataGrid.Rows.Count或DataGrid.Items.Count。wpf觸發器Datagrid RowCount

屬性「DataGrid.Rows」或「DataGrid.Items」不爲編譯器所知。你知道另一種方式嗎?我喜歡只在Xaml中使用它,而不是爲此使用Converter。 (我知道這可以通過一個轉換器來實現對itemcollection的計數)是否有更智能的方法?

<Grid> 

<DataGrid Name="dg1"> 
    <DataGrid.Columns> 
     ... 
    </DataGrid.Columns> 
</DataGrid> 
<Button Name="btn1" Visibility="Visibility"> 
</Button> 
<Grid.Triggers> 
    <Trigger SourceName="dg1" Property="DataGrid.Items.Count" Value="0"> 
     <Setter TargetName="btn1" Property="Visibility" Value="Hidden"></Setter> 
    </Trigger> 
    <Trigger SourceName="dg1" Property="DataGrid.Items.Count" Value="1"> 
     <Setter TargetName="btn1" Property="Visibility" Value="Hidden"></Setter> 
    </Trigger> 
</Grid.Triggers> 

+0

我想你鑼獲得總計數不上該行的計數。你可以把一個屬性放在你的集合中嗎? – Paparazzi

回答

1

你可以通過按鈕設置DataTrigger要顯示/隱藏,並綁定到DataGrid中的Items.Count財產。

只要集合中的物品數量達到0或1,該按鈕將被隱藏,當超過1時,它將再次顯示。

XAML:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
     <DataGrid Name="dg1" ItemsSource="{Binding MyFooCollection}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Id}" /> 
       <DataGridTextColumn Binding="{Binding Name}" /> 
      </DataGrid.Columns> 
     </DataGrid> 
     <StackPanel> 
      <Button Content="Add row" Click="btnAdd" /> 
      <Button Content="Remove row" Click="btnRemove" /> 
      <Button Content="Will be shown/hidden"> 
       <Button.Style> 
        <Style TargetType="Button"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding Items.Count, ElementName=dg1}" Value="0"> 
           <Setter Property="Visibility" Value="Hidden" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding Items.Count, ElementName=dg1}" Value="1"> 
           <Setter Property="Visibility" Value="Hidden" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Button.Style> 
      </Button> 
     </StackPanel> 
    </StackPanel> 
</Window> 

代碼隱藏:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<Foo> MyFooCollection { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     this.MyFooCollection = new ObservableCollection<Foo> 
     { 
      new Foo(1, "Bar1"), new Foo(2, "Bar2"), new Foo(3, "Bar3"), 
      new Foo(4, "Bar4"), new Foo(5, "Bar5"), new Foo(6, "Bar6") 
     }; 

     this.DataContext = this; 
    } 

    private void btnAdd(object sender, RoutedEventArgs e) 
    { 
     MyFooCollection.Add(new Foo(11, "Test1")); 
    } 

    private void btnRemove(object sender, RoutedEventArgs e) 
    { 
     if (MyFooCollection.Any()) 
      MyFooCollection.RemoveAt(0); 
    } 

    public class Foo 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 

     public Foo(int id, string name) 
     { 
      Id = id; 
      Name = name; 
     } 
    } 
} 
+0

效果很好,謝謝! – deafjeff