DataGrid绑定复选框数据

时间:2015-10-12 11:08:58

标签: c# wpf datagrid xceed

xaml代码:

<DataTemplate x:Key="GridCheckBox">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding stat, UpdateSourceTrigger=PropertyChanged}" Checked="CheckBox_Checked" Unchecked="UnCheckBox_Checked" HorizontalAlignment="Center" />
    </StackPanel>
</DataTemplate>

<xcdg:DataGridControl x:Name="_dataGrid" AllowDrag="False">
    <xcdg:DataGridControl.View>
        <xcdg:TableflowView FixedColumnCount="1" />
    </xcdg:DataGridControl.View>

    <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="." Title="Select" Width="50" IsMainColumn="True"
                     CellContentTemplate="{StaticResource GridCheckBox}"            
                     GroupValueTemplate="{StaticResource GridCheckBox}"/>
    </xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>

将数据填充到xceed datagrid

 DataGridCollectionView collectionView = new DataGridCollectionView(dt.DefaultView);
          collectionView.GroupDescriptions.Add(new DataGridGroupDescription("filter"));
          _dataGrid.ItemsSource = collectionView;

所有其他细节绑定正常但不是复选框;任何人都可以帮我解决。

2 个答案:

答案 0 :(得分:1)

在对上一个答案的评论中,您说如果要勾选stat = 1复选框。如果stat不是bool,则必须使用Converter,以便绑定到IsChecked属性。

转换器应该简单如下:

  public class StatConverter : IValueConverter
  {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      if (value is int)
      {
        return (int)value == 1;
      }

      return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }

    #endregion
  }

答案 1 :(得分:0)

在IsChecked-Binding中,尝试向您的DataContext所在的RelativeSource / Window提供UserConrolt

IsChecked-Property的绑定比看起来像:

IsChecked="{Binding DataContext.stat, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}"