简体   繁体   English

在网格上选中复选框时,GridView行不会被选中或不受影响

[英]GridView Row Does not get select or Affected When Checkbox checked on Grid

I am using WPF Application Form with Telerik Gridview using C#. 我正在使用C#将WPF申请表与Telerik Gridview一起使用。

In that Gridview i inserted checkboxes using Data template . 在那个Gridview中,我使用Data template插入了复选框。

Its created but when i click or check the checkbox the current row is not getting selected. 它创建了,但是当我单击或选中复选框时,当前行没有被选中。

How can i solve this problem? 我怎么解决这个问题?

Please anyone tell me the solution of this problem. 请任何人告诉我这个问题的解决方案。

My xaml code for creating checkbox in grid is: 我在网格中创建复选框的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>

In that checkbox i have created click event. 在该复选框中,我创建了click事件。

And also i want to know the current row number of the CheckBox Placed row in the gridview in checkbox click event . 而且我也想知道在checkbox click事件中gridview中CheckBox Placed行的当前行号 Give me some suggestion for this. 给我一些建议。

Here My CheckBox Click event Code: 这是我的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
}

Thanks in Advance. 提前致谢。

First of all, I don't see you actually setting the row as Selected anywhere. 首先,我看不到您实际将行设置为“已Selected

Your binding currently is setting a property in your DataContext called IsSelected . 您的绑定当前正在您的DataContext中设置一个名为IsSelected的属性。 If this property actually exists, you can bind it to GridViewRow.IsSelected in a style 如果此属性确实存在,则可以GridViewRow.IsSelected样式将其绑定到GridViewRow.IsSelected

<Style TargetType="{x:Type telerik:GridViewRow}">
    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>

If it doesn't exist in your data object, you need to use a RelativeSource binding to find the GridViewRow and bind to it's IsSelected property 如果数据对象中不存在该对象,则需要使用RelativeSource绑定来查找GridViewRow并绑定到其IsSelected属性

Checked="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:GridViewRow}}, Path=IsSelected}"

As for finding the row number, there is no RowIndex property I know of that you can use to find the row number. 至于查找行号,我知道没有RowIndex属性可用于查找行号。 One alternative is to get the GridView's ItemsSource and call IndexOf(dataobject) , however that does break the separation of MVVM layers 一种替代方法是获取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
}

There are also other alternatives such as setting AlternationCount to something higher than the the number of rows, and accessing GridViewRow.AlternationIndex , although I don't know if that will work with Telerik's GridView 还有其他选择,例如将AlternationCount设置为比行数更高的值,并访问GridViewRow.AlternationIndex ,尽管我不知道这是否适用于Telerik的GridView

But perhaps the best thing you could do is evaluate what you actually need the index for, and see if you can use some other alternative to accomplish what you want. 但是也许您能做的最好的事情就是评估您实际需要索引的内容,并查看是否可以使用其他替代方法来完成所需的操作。 For example, if you want to pass the index of the selected row to a button command, it would be better to just pass the SelectedItem as the CommandParameter. 例如,如果要将选定行的索引传递给按钮命令,最好将SelectedItem作为CommandParameter传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM