是Silverlight DataGrid复选框事件吗? [英] Are Silverlight DataGrid Checkbox events?

查看:152
本文介绍了是Silverlight DataGrid复选框事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用绑定的DataGridCheckBoxColumn调用点击方法吗?或者事件是行级别还是行输入事件?

Is it possible to have a bound DataGridCheckBoxColumn call a method of click? Or event a row level or row enter event?

我正在尝试实现一个更新底层过滤的ObservableCollection集合的datagrid(这里是伟大的单片机 http://petermcg.wordpress.com/2009/01/29/filtering-silverlight -datagrid-rows /

I'm trying to implement a datagrid that updates the underlying filtered ObservableCollection collection (great sameple here http://petermcg.wordpress.com/2009/01/29/filtering-silverlight-datagrid-rows/)

如果我按更新按钮或其他外部方法,则过滤功能正常。但是,我似乎找不到任何内部网格事件或DataGridCheckBoxColumn上允许我调用更新过滤器逻辑的任何内容。

The filtering works fine if I press an update button or another outside method. But, I can't seem to find any internal grid event or anything on the DataGridCheckBoxColumn that will allow for me to call the update filter logic.

有关如何实现数据网格的任何建议,可以按复选框更新基础集合?

Any suggestions on how to implement a datagrid that updates the underlying collection on press of a checkbox?

请找到我的Xaml下面,这真的不是什么特别的。

Please find below my Xaml, it really isn't anything special.









推荐答案

DataGridCheckBoxColumn 类型当前不提供Click事件,实际上它目前不提供任何事件。在此类型的列中呈现的单元格仅实现了CheckBox控件的IsChecked属性与ObservableCollection< T>的当前行中的T实例上的属性(在Binding中指定)之间的TwoWay绑定。 DataGrid绑定到。

The DataGridCheckBoxColumn Type does not currently provide a Click event, in fact it doesn't currently provide any events. A cell rendered in a column of this Type just implements a TwoWay binding between the IsChecked property of a CheckBox control and the property (specified in the Binding) on the instance of T in the current row of the ObservableCollection<T> that the DataGrid is bound to.

您不提供您的Xaml,所以我将使用我的博客帖子中引用您的问题的示例。解决方法涉及指定 DataGridTemplateColumn 包含CheckBox并定义Click事件如下:

You don't provide your Xaml so I'll use the example from my blog post referenced in your question. The workaround involves specifying a DataGridTemplateColumn containing a CheckBox and defining the Click event as follows:

<data:DataGrid x:Name="FilteredPeople" AutoGenerateColumns="False">
  <data:DataGrid.Columns>
    <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
    <data:DataGridTextColumn Header="Age" Binding="{Binding Age}" />
    <data:DataGridCheckBoxColumn Header="Visible" Binding="{Binding IsVisible}" />

    <data:DataGridTemplateColumn Header="Row Filter">
      <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox x:Name="RowFilterButton" IsChecked="{Binding IsVisible}"
                Content="Filter" Tag="{Binding}" Click="RowFilterButton_Click" />
        </DataTemplate>
      </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>

  </data:DataGrid.Columns>
</data:DataGrid>

在这种情况下,CheckBox的替代方法可能是ToggleButton或正常的Button控件。注意CheckBox的Tag属性的绑定。这有助于找出从代码隐藏中点击CheckBox的哪一行:

An alternative to a CheckBox in this case could be a ToggleButton or normal Button control. Notice the binding for the Tag property of the CheckBox. This helps to find out in which row the CheckBox was clicked from code-behind:

private void RowFilterButton_Click(object sender, RoutedEventArgs e)
{
    Person person = ((CheckBox)sender).Tag as Person;
}

然后,您可以从ObservableCollection&T;或者更改其属性以适当地过滤或更新DataGrid。

You can then remove this person from the ObservableCollection<T> or change it's properties to filter or update the DataGrid as appropriate.

通常情况下,此答案将会结束,但如果您没有安装此更新有一个错误在默认的SDK版本的Silverlight 2 DataGrid,阻止这一点。在这种情况下,如果您单击该复选框(即复选框具有焦点)导致DataGrid被过滤,则DataGrid将不会绘制CheckBox所在的行(而是您看起来像一个不可见行) 。

That's normally where this answer would end but if you do not have this update installed there is a bug in the default SDK version of the Silverlight 2 DataGrid that prevents this. In this case the bug will manifest itself by the DataGrid not drawing the row the CheckBox is in (instead what you get looks like an invisible row) if you cause the DataGrid to be filtered when you click that CheckBox (i.e. the Checkbox has focus).

为避免此错误,请安装更新或启动对ObservableCollection< T> DataGrid绑定到DataGrid行外的一个控件,根据这个例子

To avoid this bug, install the update or initiate any manipulation of the ObservableCollection<T> the DataGrid is bound to from a Control outside the DataGrid's rows as per this example.

这篇关于是Silverlight DataGrid复选框事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆