locked
Wpf 4.0 Datagrid Problem RRS feed

  • Question

  • We are using Wpf4.0 Datagrid and in one of the column we have check box.

    Is there any way to check the check box with single click,currently it needs two mouse clicks one to select row and other one to check check box.I don't want to use any code behind as i am using MVVM light.

    Wednesday, July 10, 2013 4:21 PM

Answers

  • Hi,

    You haven't done it the way Lisa Zhu wrote it. You templated a CheckBox column. Read Lisa Zhu's post more carefuly again, and you will notice there's a DataGridTemplate column with a DataTemplate inside not a CheckBoxColumn. It all boils down to:

    <DataGridTemplateColumn>
       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
             <CheckBox/>
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    Good luck,

    Best regards,

    Simon

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:29 AM
    Wednesday, July 24, 2013 1:28 PM
  • Hi Balaji,

    Please use DataGridTemplateColumn to "hold" a CheckBox instead of using DataGridCheckBoxColumn.

    Something like this:

     <DataGrid Name="dg">           
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsCompelte }"></CheckBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn> 
                    <!--<DataGridCheckBoxColumn Binding="{Binding IsCompelte}"></DataGridCheckBoxColumn>-->
                </DataGrid.Columns>
            </DataGrid>
    Regards,

    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Simon Katanski Thursday, July 25, 2013 8:18 AM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:29 AM
    Thursday, July 11, 2013 9:31 AM

All replies

  • Hi Balaji,

    Please use DataGridTemplateColumn to "hold" a CheckBox instead of using DataGridCheckBoxColumn.

    Something like this:

     <DataGrid Name="dg">           
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsCompelte }"></CheckBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn> 
                    <!--<DataGridCheckBoxColumn Binding="{Binding IsCompelte}"></DataGridCheckBoxColumn>-->
                </DataGrid.Columns>
            </DataGrid>
    Regards,

    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Simon Katanski Thursday, July 25, 2013 8:18 AM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:29 AM
    Thursday, July 11, 2013 9:31 AM
  • Hi,

    I have tried your approch by including check box control inside datagridtemplate column  but still it need to mouse clicks to check it.

    My datagrid looks like as below.Can you please suggest.

           <DataGrid x:Uid="DataGrid_1"
                              Tag="P"
                              DockPanel.Dock="Top"
                              Margin="5"
                              AllowDrop="True"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Top"
                              AutoGenerateColumns="False"
                              CanUserAddRows="False"
                              CanUserDeleteRows="{Binding ElementName=ProcessingTabViewControl, Path=SelectedProcessingIdentifier.UserDefined}"                          
                              ItemsSource="{Binding ElementName=ProcessingTabViewControl, Path=ProcessingIdentifierItemSource,  Mode=TwoWay}"
                              SelectedItem="{Binding ElementName=ProcessingTabViewControl, Path=SelectedProcessingIdentifier,   Mode=TwoWay}"> 
                        <i:Interaction.Behaviors>
                                <behaviors:DataGridDropBehavior/>
                                <behaviors:ScrollIntoViewBehavior/>
                        </i:Interaction.Behaviors>
                                
                        <DataGrid.Columns>
                            <DataGridTemplateColumn x:Uid="DataGridTemplateColumn_1" Header="Actions" IsReadOnly="True">
                                            <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate x:Uid="DataTemplate_1">
                                        <Button x:Uid="Button_2"
                                                MaxWidth="27"
                                                MaxHeight="27"
                                                IsEnabled="{Binding Path=UserDefined}"
                                                Command="Delete">
                                                        <Image x:Uid="Image_1" 
                                                   Source="../Resources/Images/imgRemove.png"  
                                                   Width="10"  
                                                   Height="10" />
                                        </Button>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn x:Uid="DataGridTextColumn_1" 
                                                MinWidth="150"
                                                Binding="{Binding Path=Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
                                                IsReadOnly="{Binding Path=UserDefined}"
                                                Header="Identifier">
                                <DataGridTextColumn.EditingElementStyle>
                                    <Style TargetType="TextBox">
                                                    <Setter Property="IsEnabled" Value="{Binding Path=UserDefined}"/>
                                                        <Setter Property="AllowDrop" Value="True" />
                                                    <EventSetter Event="PreviewDragOver"  Handler="PreviewDragOverHandler"/>
                                                    <Style.Triggers>
                                                        <Trigger Property="Validation.HasError" Value="true">
                                                            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
                                                        </Trigger>
                                                    </Style.Triggers>
                                    </Style>
                                </DataGridTextColumn.EditingElementStyle>
                            </DataGridTextColumn>
                            <!--<DataGridCheckBoxColumn x:Uid="DataGridCheckBoxColumn_1"
                                                    Binding="{Binding Path=IsPattern}" 
                                                    Header="Pattern" 
                                                    >
                                <DataGridCheckBoxColumn.ElementStyle>
                                    <Style TargetType="CheckBox">
                                        <Setter Property="VerticalAlignment" Value="Center" />
                                        <Setter Property="HorizontalAlignment"  Value="Center" />
                                    </Style>
                                </DataGridCheckBoxColumn.ElementStyle>
                            </DataGridCheckBoxColumn>-->
                                        <DataGridTemplateColumn Header="Include">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <RadioButton  VerticalAlignment="Center" 
                                                                  HorizontalAlignment="Center"    
                                                                  IsChecked="{Binding Path=IncludeIdentifier, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                                    
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Header="Exclude">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <RadioButton  VerticalAlignment="Center" 
                                                                  HorizontalAlignment="Center" 
                                                                  IsChecked="{Binding Path=ExcludeIdentifier, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                            <DataGridCheckBoxColumn x:Uid="DataGridCheckBoxColumn_4" 
                                                    Binding="{Binding Path=Active, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="Active" 
                                            .ElementStyle>
                                                <Style TargetType="CheckBox">
                                        <Setter Property="V        >
                                            <DataGridCheckBoxColumnerticalAlignment" Value="Center" />
                                        <Setter Property="HorizontalAlignment"  Value="Center" />
                                     </Style>
                                                </DataGridCheckBoxColumn.ElementStyle>
                            </DataGridCheckBoxColumn>
                        </DataGrid.Columns>
                    </DataGrid>

    Monday, July 15, 2013 6:48 AM
  • Hello team can you please reply.
    Wednesday, July 24, 2013 11:39 AM
  • Hi,

    You haven't done it the way Lisa Zhu wrote it. You templated a CheckBox column. Read Lisa Zhu's post more carefuly again, and you will notice there's a DataGridTemplate column with a DataTemplate inside not a CheckBoxColumn. It all boils down to:

    <DataGridTemplateColumn>
       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
             <CheckBox/>
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    Good luck,

    Best regards,

    Simon

    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:29 AM
    Wednesday, July 24, 2013 1:28 PM