【问题标题】:The checkbox in the DataGridTemplateColumn is malfunctionalDataGridTemplateColumn 中的复选框出现故障
【发布时间】:2012-07-31 16:24:49
【问题描述】:

我在 Silverlight 中的 Datagrid 中添加了一个带有复选框的 DataGridTemplateColumn。 Checkbox 作为列成功添加到数据网格中。但是,如果我选中了多个 chcekbox,则此列中的其他一些复选框将自动受到影响(即,从选中更改为未选中,反之亦然。)。我没有为此复选框添加任何事件处理程序,那么这怎么会发生呢?在我看来,如果我滚动数据网格以查看更多记录,数据网格会自动刷新/更新,此时复选框会混乱。我的代码如下:

            DataGridTemplateColumn templateColumn = new DataGridTemplateColumn() { Header = "header",Width=new DataGridLength(50) };               
            StringBuilder CellTemp = new StringBuilder();
            CellTemp.Append("<DataTemplate ");
            CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
            CellTemp.Append("2006/xaml/presentation' ");
            CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >");
            CellTemp.Append("<CheckBox  x:Name='" + "checkBoxHeader" + "' Margin='4'  IsChecked='False'/>");
            CellTemp.Append("</DataTemplate>");
            templateColumn.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());
            ftrDataGrid.Columns.Add(templateColumn);

我应该如何解决这个问题,我应该向这个数据网格添加一个事件处理程序,但是如何解决呢?

非常感谢您的意见,

【问题讨论】:

  • 那是因为 DataGrid 期待一个提交,而它不能通过单击获得。如果绑定数据,请将绑定的 Update 属性更改为 PropertyChanged。可能是 Silverlight 5,但也可能在 4 中
  • 谢谢,Silvermind。我不擅长装订。我应该如何绑定到复选框?初始复选框与附加到数据网格的数据源中的任何属性都没有关联,我只想首先取消选中所有属性。我应该使用字典来记录/绑定已检查的内容吗?
  • 也许您可以使用@TriggerPin 给出的示例,但您必须遵循我之前的评论。如果您的数据与您的数据源不相关,那么您可能需要考虑为其创建一个包装类,即所谓的 ViewModel。

标签: c# silverlight datagrid


【解决方案1】:

在模板中设置绑定就这么简单:

DataGridTemplateColumn templateColumn = new DataGridTemplateColumn() { Header 
...

CellTemp.Append("<CheckBox  x:Name='" + "checkBoxHeader" + "' Margin='4'  IsChecked="{Binding MyProperty, Mode=TwoWay}"/>");
...            
ftrDataGrid.Columns.Add(templateColumn);

其中 MyProperty 是布尔值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多