【问题标题】:Single-click-editable bound CheckBox in Xceed DataGridControlXceed DataGridControl 中的单击可编辑绑定 CheckBox
【发布时间】:2016-10-03 22:54:56
【问题描述】:

过去一个小时我一直在寻找一个简单问题的解决方案:如何在 Xceed 的社区 DataGridControl 中创建单击可编辑的绑定 CheckBox

明确一点:我想要一个CheckBox 列,用户可以在其中单击任何CheckBox,而不管选择了哪一行,并相应地更改视图模型的IsSelected 属性。

以下是我尝试过的最新排列。此代码从模型中读取值,但由于某种原因,单击 CheckBox 不会调用 IsSelected 设置器。

<xcdg:DataGridControl x:Name="DictionariesDataGridControl" ItemsSource="{Binding Mode=OneWay, Source={StaticResource DictionariesViewSource}}" AutoCreateColumns="False" AutoRemoveColumnsAndDetailConfigurations="False" SelectionMode="Extended" NavigationBehavior="RowOnly">
    <xcdg:DataGridControl.View>
        <xcdg:TableView UseDefaultHeadersFooters="False" ShowRowSelectorPane="False" VerticalGridLineThickness="0">
            <xcdg:TableView.FixedHeaders>
                <DataTemplate>
                    <xcdg:ColumnManagerRow BorderThickness="0"/>
                </DataTemplate>
            </xcdg:TableView.FixedHeaders>
        </xcdg:TableView>
    </xcdg:DataGridControl.View>
    <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="IsSelected" MinWidth="20" MaxWidth="20" CellEditorDisplayConditions="RowIsCurrent">
            <xcdg:Column.CellContentTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding ., Mode=OneWay}" IsHitTestVisible="False"/>
                </DataTemplate>
            </xcdg:Column.CellContentTemplate>
            <xcdg:Column.CellEditor>
                <xcdg:CellEditor>
                    <xcdg:CellEditor.EditTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding ., Mode=TwoWay}"/>
                        </DataTemplate>
                    </xcdg:CellEditor.EditTemplate>
                </xcdg:CellEditor>
            </xcdg:Column.CellEditor>
        </xcdg:Column>
    </xcdg:DataGridControl.Columns>

编辑 1

我正在尝试这个,这正是我需要的:

<xcdg:Column FieldName="IsSelected" MinWidth="20" MaxWidth="20" CellEditorDisplayConditions="Always"/>

除了,由于某种原因,CheckBox 被设置为蓝色背景!

我在可视化树中选择了一个元素,该元素具有定义为SolidColorBrushBackground 属性,颜色为#FF0000FF

编辑 2

我反编译了 Xceed 用来渲染 CheckBoxDataGridCheckBox 类,发现了这个覆盖:

public override void OnApplyTemplate()
{
  base.OnApplyTemplate();
  this.ChildCheckBox.Background = (Brush) new SolidColorBrush(Colors.Blue);
}

Xceed 任意将背景颜色设置为蓝色是多么奇怪的决定。

编辑 3

使用@JBrooks 的回答,我尝试了以下方法:

<xcdg:Column FieldName="IsSelected" MinWidth="20" MaxWidth="20" CellEditorDisplayConditions="Always">
    <xcdg:Column.CellEditor>
        <xcdg:CellEditor>
            <xcdg:CellEditor.EditTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding ., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </xcdg:CellEditor.EditTemplate>
        </xcdg:CellEditor>
    </xcdg:Column.CellEditor>
</xcdg:Column>

不幸的是,由于某种原因,当我选中该框时,从未调用过 IsSelected 属性上的设置器。不过,getter 被多次调用,CheckBoxes 在初始绑定时正确显示。

【问题讨论】:

  • 这显然是DataGridCheckBox 中的一个已知错误。这么多代码审查...wpftoolkit.codeplex.com/workitem/20210
  • “这个”是指蓝色背景的愚蠢。
  • 3个问题:输出窗口有什么东西吗?您是否有类似于捕获的事件 CurrentCellChanged 的​​内容?当复选框失去焦点时,setter 会被设置吗?
  • 1.输出窗口中没有任何内容,没有。不过,我没有打开任何特殊的日志记录。 2. 我明确连接到的唯一事件是ItemsSourceChangeCompleted,以便填充集合视图的SortDescriptions 集合。 3. 无论我如何操作复选框,包括更改选择、使用TAB 键等,setter 都不会被调用。

标签: wpf checkbox xceed-datagrid


【解决方案1】:

您同时拥有 CellContentTemplate 和 CellEditor,因此第一次单击会执行“进入编辑模式”逻辑。只要有一个像下面这样的。这是一个普通的 WPF DataGrid,但也许你可以为你的网格尝试类似的东西。

  <DataGridTemplateColumn Header="Active" SortMemberPath="IsActive"  >
     <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsEnabled}" Style="{StaticResource rightsCB}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

对于这个 DataGrid,我还设置了以下属性:

SelectedItem="{Binding SelectedUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="FullRow" SelectionMode="Single"

所以这个 DataGrid 的行为就像你想要的那样 - 我点击第 4 行中的复选框,IsChecked 发生变化,它还使第 4 行成为当前行,将 SelectedUser 设置为绑定到第 4 行的用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 2015-08-11
    • 1970-01-01
    • 2018-07-15
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多