简体   繁体   中英

WPF DefaultButton not working as expected

I have the following set of controls on a form:

在此处输入图片说明

Each of them is a separate user control, and buttons are set to IsDefault="True" .

When typing in the "Quick Search" textbox and pressing enter, the focus jumps from this control to the "..." button on "CDV check".

在此处输入图片说明

How is this possible? Am I missing something or is this a problem within WPF?

Thanx

You can achieve this using style triggers , please look at the sample xaml below. if you want you can move the style common to the resources.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM