2011-07-20 164 views
7

我有一個DataGrid,顯示一堆對象。這些對象有一個屬性IsDetailsExpanded,我想將DataRows DetailsVisibility屬性綁定到該屬性。WPF DataGrid RowDetails綁定到屬性的可見性(僅限XAML)

我的第一種方法有效,但需要一些代碼隱藏(我想擺脫)

我處理LoadingRow事件

void LoadingRowHandler(object sender, DataGridRowEventArgs e) 
{ 
    Binding b = new Binding() 
    { 
     Source = e.Row.DataContext, 
     Path = new PropertyPath("IsExpanded"), 
     Converter = (IValueConverter)Resources["BoolToVisi"], 
     Mode = BindingMode.TwoWay 
    }; 
    e.Row.SetBinding(DataGridRow.DetailsVisibilityProperty, b); 
} 

我認爲必須有一種方式來實現在XAML中類似的東西,但我不幸我沒有絲毫的線索。有任何想法嗎?建議?

回答

14

您可以使用StyleDataGridRow類型,就像這樣:

<DataGrid Name="dataGrid1" Margin="12,12,0,0"> 
    <DataGrid.RowStyle> 
     <Style TargetType="DataGridRow"> 
      <Setter Property="DetailsVisibility" Value="{Binding IsExpanded, Converter={StaticResource BoolToVisi}}" /> 
     </Style> 
    </DataGrid.RowStyle> 
</DataGrid> 
+0

沒錯。這是xaml的方式。只有屬性應該等於DetailsVisibility。 –

+0

@Yiğit - 感謝您指出我的複製和粘貼錯誤:-) – CodeNaked

+0

有時我想濫用此評論功能並放棄一些隨意的笑臉。是否皺眉::)))) –