2013-06-05 70 views
0

我想在WPF MVVM中創建一個具有信息行的數據網格,而列是代表Boolean屬性的DataGridCheckBoxColumn如何在一次點擊中禁用行選擇並啓用複選框?

我希望能夠點擊一個複選框,並將其更改爲「檢查」在一次點擊。 我也想禁用選項來選擇行,也禁用選項來更改其他列中的其他內容。

請指教。

回答

0

使用該答案爲出發點:How to perform Single click checkbox selection in WPF DataGrid?

我做了一些修改,並結束了與此:

WPF:

<DataGrid.Resources> 
    <Style TargetType="{x:Type DataGridRow}"> 
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridRow_PreviewMouseLeftButtonDown"/> 
    </Style> 
    <Style TargetType="{x:Type DataGridCell}"> 
    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/> 
    </Style> 
</DataGrid.Resources> 

後面的代碼:

private void DataGridRow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     DataGridRow row = sender as DataGridRow; 
     if (row == null) return; 
     if (row.IsEditing) return; 
     if (!row.IsSelected) row.IsSelected = true; // you can't select a single cell in full row select mode, so instead we have to select the whole row 
    } 

    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     DataGridCell cell = sender as DataGridCell; 
     if (cell == null) return; 
     if (cell.IsEditing) return; 
     if (!cell.IsFocused) cell.Focus(); // you CAN focus on a single cell in full row select mode, and in fact you HAVE to if you want single click editing. 
     //if (!cell.IsSelected) cell.IsSelected = true; --> can't do this with full row select. You HAVE to do this for single cell selection mode. 
    } 

嘗試一下,看看它是否做到了你想要的。

相關問題