[英]GridView Row Does not get select or Affected When Checkbox checked on Grid
我正在使用C#將WPF申請表與Telerik Gridview一起使用。
在那個Gridview中,我使用Data template插入了復選框。
它創建了,但是當我單擊或選中復選框時,當前行沒有被選中。
我怎么解決這個問題?
請任何人告訴我這個問題的解決方案。
我在網格中創建復選框的xaml代碼是:
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox Name="CheckBox" IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}" Click="CheckBox_Click" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
在該復選框中,我創建了click事件。
而且我也想知道在checkbox click事件中gridview中CheckBox Placed行的當前行號 。 給我一些建議。
這是我的CheckBox Click事件代碼:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox selectedCheckbox = (CheckBox)sender;
//this.selectedCasePackRadGrid -- This is my gridview
// Here I want to get the selected row number
}
提前致謝。
首先,我看不到您實際將行設置為“已Selected
。
您的綁定當前正在您的DataContext中設置一個名為IsSelected
的屬性。 如果此屬性確實存在,則可以GridViewRow.IsSelected
樣式將其綁定到GridViewRow.IsSelected
<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
如果數據對象中不存在該對象,則需要使用RelativeSource
綁定來查找GridViewRow
並綁定到其IsSelected
屬性
Checked="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:GridViewRow}}, Path=IsSelected}"
至於查找行號,我知道沒有RowIndex
屬性可用於查找行號。 一種替代方法是獲取GridView的ItemsSource
並調用IndexOf(dataobject)
,但這確實打破了MVVM層的分離
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
var dataItem = checkBox.DataContext as MyDataItem;
var parentDataObject = myGridView.ItemsSource as SomeDataObject;
if (dataItem == null || parentDataObject == null)
return;
var index = parentDataObject.SomeCollection.IndexOf(dataItem);
// Do something with index
}
還有其他選擇,例如將AlternationCount
設置為比行數更高的值,並訪問GridViewRow.AlternationIndex
,盡管我不知道這是否適用於Telerik的GridView
但是也許您能做的最好的事情就是評估您實際需要索引的內容,並查看是否可以使用其他替代方法來完成所需的操作。 例如,如果要將選定行的索引傳遞給按鈕命令,最好將SelectedItem
作為CommandParameter傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.