Rashmi S
Rashmi S

Reputation: 267

DataGrid Binding checkbox data

xaml code:

<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>

Filling a data to xceed datagrid

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

All other details binding fine but not checkbox;Can anyone help me to solve.

Upvotes: 0

Views: 455

Answers (2)

Giangregorio
Giangregorio

Reputation: 1510

In a comment to the previous answer you say that if stat=1 checkbox should be ticked. If stat is not a bool, you have to use a Converter so you can get bind to IsChecked property.

The converter should be simple as:

  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
  }

Upvotes: 1

Tomtom
Tomtom

Reputation: 9394

In your IsChecked-Binding try to provide a RelativeSource to your Window/UserConrolt where your DataContext is.

The binding for the IsChecked-Property than looks something like:

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

Upvotes: 0

Related Questions