How to set up the datagrid control from the Xceed\

2019-02-18 11:44发布

问题:

I'm trying to swap out a WPF datagrid to a xceed\Extended WPF Toolkit DataGridControl.

I need to react to the click event in a checkbox column ... to summarizing a number of other columns.

In the existing datagrid I have a checkbox column, that is bound to a Observable Collection and I call a method if any check box is checked\unchecked. The xaml I use for this, which works, is as such:

<DataGridTemplateColumn Width="40" Header="Inc">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox
            IsChecked="{Binding Include ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
            Checked="CheckBoxUpdated" Unchecked="CheckBoxUpdated" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

For the xceed datagridcontrol I started with the simple syntax below, and the the initial binding seemed OK, but I don't have a click event to respond to:

<xcdg:Column   FieldName="Include" Title="Inc" />

Now I tried to do something similar to the original code using the xceed datagridcontrol, as such:

<xcdg:Column   FieldName="Include" Title="Inc" Width="*" >
<xcdg:Column.CellContentTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Include ,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Click="CheckBoxUpdated"/>
    </DataTemplate>
</xcdg:Column.CellContentTemplate>

But I don't think this is the correct syntax. It seems the binding is not working ... based on the initial values of the collection.

(note code behind sets this items source as such dg.ItemsSource = collectionView;)

Any ideas on how to setup a checkbox DataTemplate and the binding correctly?

Thanks

回答1:

I just found a post at xceed forums that gave me the syntax I needed, that to set the FieldName=".", not FieldName="Include" . My guess is that having FieldName="Include" and the "{Binding Include ..." was confusing the binding.

<xcdg:Column   FieldName="." Title="Inc" Width="*" >
 <xcdg:Column.CellContentTemplate>
  <DataTemplate>
    <CheckBox IsChecked="{Binding Include ,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Click="CheckBoxUpdated"/>
</DataTemplate>
</xcdg:Column.CellContentTemplate>

share|improve this answer
  • this works! path should be ., and full path in template. – Lei Yang Jun 15 '16 at 15:32
  • This only works if you are binding to a single field that you need to do this trick with. Otherwise it will only let you bind one column to FieldName ="." – markokstate Feb 15 '17 at 22:11
2

Your solution to your question wasn't working for me, what did work however:

Either

<xcdg:Column ...

if the type is boolean it will automaticly create a checkbox for it, you'll have to click 3 times though (column edit -> (un)check -> column out) which can be annoying.

OR

<xcdg:Column FieldName="ckb1" DisplayMemberBinding="{Binding Path=IsThisChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                    <xcdg:Column.CellContentTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{xcdg:CellEditorBinding NotifyOnSourceUpdated=True}" HorizontalAlignment="Center" />
                        </DataTemplate>
                    </xcdg:Column.CellContentTemplate>
                </xcdg:Column>

Which doesn't need all the clicking

share|improve this answer
  • Unfortunately, Xceed decided to leave a line of code in their DataGridCheckBox.OnApplyTemplate method: this.ChildCheckBox.Background = (Brush) new SolidColorBrush(Colors.Blue); If you like your check boxes with arbitrary blue backgrounds that aren't overrideable, go for it! :) – NathanAldenSr Oct 5 '16 at 0:20
  • fixed in version 6.1.16565.14160, see xceed.com/release-notes – Cauly Nov 29 '16 at 2:20
  • Did they Obsolete 'DisplayMemberBinding'? I only see DisplayMemberBindingInfo which I cannot bind to. – markokstate Feb 15 '17 at 22:15

Your Answer

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged wpf checkbox wpftoolkit or ask your own question.

收藏的人(0)

Ta的文章 更多文章
登录 后发表评论
0条评论
还没有人评论过~