WPF DefaultButton无法按预期工作

时间:2013-01-22 11:07:31

标签: c# wpf xaml

我在表单上有以下控件集:

enter image description here

每个都是一个单独的用户控件,按钮设置为IsDefault="True"

在“快速搜索”文本框中输入并按Enter键时,焦点会从此控件跳到“CDV检查”上的“...”按钮。

enter image description here

这怎么可能?我错过了什么或者这是WPF中的问题吗?

感谢名单

2 个答案:

答案 0 :(得分:1)

您可以使用样式触发器实现此目的,请查看下面的示例xaml。如果你愿意,你可以移动资源共有的风格。

<GroupBox Header="GroupBox1"
          Height="100"
          HorizontalAlignment="Left"
          Margin="35,49,0,0"
          Name="GroupBox1"
          VerticalAlignment="Top"
          Width="247">
  <Grid>
    <TextBox Height="23"
             HorizontalAlignment="Left"
             Margin="10,30,0,0"
             x:Name="TextBox1"
             VerticalAlignment="Top"
             Width="120" />

    <Button Content="Button1"
            Height="23"
            HorizontalAlignment="Left"
            Margin="136,29,0,0"
            Name="Button1"
            VerticalAlignment="Top"
            Width="75">
      <Button.Style>
        <Style  TargetType="Button">
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=TextBox1, Path=IsFocused }"
                         Value="True">
              <Setter Property="IsDefault"
                      Value="True"></Setter>
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
    </Button>
  </Grid>
</GroupBox>
<GroupBox Header="GroupBox2"
          Height="98"
          HorizontalAlignment="Left"
          Margin="35,155,0,0"
          Name="GroupBox2"
          VerticalAlignment="Top"
          Width="247">
  <Grid>
    <TextBox Height="23"
             HorizontalAlignment="Left"
             Margin="10,29,0,0"
             x:Name="TextBox2"
             VerticalAlignment="Top"
             Width="120" />
    <Button Content="Button"
            Height="23"
            HorizontalAlignment="Left"
            Margin="136,29,0,0"
            Name="Button2"
            VerticalAlignment="Top"
            Width="75">
      <Button.Style>
        <Style  TargetType="Button">
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=TextBox2, Path=IsFocused }"
                         Value="True">
              <Setter Property="IsDefault"
                      Value="True"></Setter>
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
    </Button>
  </Grid>

答案 1 :(得分:0)

尝试在每个父版式控件上设置FocusManager.IsFocusScope="true"