DataGrid的CellEditingTemplate并专注于编辑模式

时间:2022-03-22 22:17:25

I am having an issue with WPFToolkit DataGrid when a column is customized supplying both CellTemplate and CellEditingTemplate. If you take a look below, you will see my editing template has a single CheckBox. All is fine in a functional sense but when F2 is hit to edit the cell, one must also hit TAB in order for the CheckBox to receive focus. Ideally, one would hit F2 and SPACE to toggle the value. Currently, one must hit F2, TAB, SPACE. I have tried setting TabIndex to no avail. I am running out of ideas.

当自定义列提供CellTemplate和CellEditingTemplate时,我遇到了WPFToolkit DataGrid的问题。如果你看一下下面,你会看到我的编辑模板有一个CheckBox。在功能意义上一切都很好,但是当点击F2来编辑单元格时,还必须点击TAB才能使CheckBox获得焦点。理想情况下,人们会点击F2和SPACE来切换值。目前,必须打F2,TAB,SPACE。我试过设置TabIndex无济于事。我的想法已经不多了。

<WPFToolkit:DataGridTemplateColumn Header="Turn"
                                   MinWidth="60">
    <WPFToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Height="16">
                <Image.Style>
                    <Style TargetType="{x:Type Image}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding CanTurn}" Value="True">
                                <Setter Property="Source" Value="/Images/16/Tick.png" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellTemplate>

    <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
        </DataTemplate>
    </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFToolkit:DataGridTemplateColumn>

3 个解决方案

#1


22  

Try this

尝试这个

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <CheckBox Name="checkbox" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
        <DataTemplate.Triggers>
            <Trigger SourceName="checkbox" Property="IsVisible" Value="True">
                <Setter TargetName="checkbox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=checkbox}" />
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

#2


12  

Or this...

或这个...

<DataGridTemplateColumn Header="Long" IsReadOnly="False" Width="100">
  <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <CheckBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
    </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

#3


1  

if you want to set the focus on edit and select the text given by a Binding try this.

如果你想把焦点放在编辑上并选择一个Binding给出的文本试试这个。

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Parameter0, Mode=TwoWay}" Loaded="TbLoaded" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

code behind:

代码背后:

private void TbLoaded(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb == null) return;

    tb.SelectAll();
    FocusManager.SetFocusedElement(this, tb);
}

#1


22  

Try this

尝试这个

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <CheckBox Name="checkbox" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
        <DataTemplate.Triggers>
            <Trigger SourceName="checkbox" Property="IsVisible" Value="True">
                <Setter TargetName="checkbox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=checkbox}" />
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

#2


12  

Or this...

或这个...

<DataGridTemplateColumn Header="Long" IsReadOnly="False" Width="100">
  <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <CheckBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
    </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

#3


1  

if you want to set the focus on edit and select the text given by a Binding try this.

如果你想把焦点放在编辑上并选择一个Binding给出的文本试试这个。

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Parameter0, Mode=TwoWay}" Loaded="TbLoaded" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

code behind:

代码背后:

private void TbLoaded(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb == null) return;

    tb.SelectAll();
    FocusManager.SetFocusedElement(this, tb);
}