3

xamlのボタンにフォーカスを変更するのに問題があります。私が試みているコードは次のようになります(いくつかの条件が満たされている場合は、フォーカスをボタンに設定する必要があります。奇妙なことに、テスト目的でボタンの背景も変更しており、このプロパティはそれぞれに設定されています条件が満たされる時間デフォルトのボタンを設定したり、そのボタンにフォーカスを設定したりするにはどうすればよいですか?

<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
  <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding Path=SomeProperty1.Count, Converter={StaticResource IntegerToBooleanConverter}}" Value="True"/>
      <Condition Binding="{Binding Path=SomeProperty2, Converter={StaticResource NullToBoolConverter}}" Value="False"/>
      <Condition Binding="{Binding Path=SomeProperty3.Count, Converter={StaticResource IntegerToBooleanConverter}}" Value="True"/> 
    </MultiDataTrigger.Conditions>
    <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
    <Setter Property="IsDefault" Value="True"/> 
    <Setter Property="Background" Value="Green"/>
  </MultiDataTrigger>
</Style.Triggers>

さらに、SomeProperty1とSomeProperty2は、特定のボタンをクリックした場合にのみ設定されることを書きたいと思います。ご覧のとおり、これらのボタンにフォーカスがあります。

4

1 に答える 1

3

問題は、FocusManager.FocusedElement内のローカル フォーカスのみを制御することFocusScopeです。Buttonは独自の FocusScope ではないため、効果はありません。Focus() メソッドを呼び出す必要があるため、コードを記述する必要があります。

明白なことを行ってイベント ハンドラーを作成するか、明白でないことを行って、false から true に移行するときに を設定する添付プロパティ "MyFocusManager.ForceFocus" を作成することができますFocusManager.FocusedElement。これはPropertyChangedCallback、次のようなもので行われます。

public class MyFocusManager
{
  public static bool GetForceFocus .... // use "propa" snippet to fill this in
  public static void SetForceFocus ....
  public static DependencyProperty ForceFocusProperty = DependencyProperty.RegisterAttached("ForceFocus", typeof(bool), typeof(MyFocusManager),  new UIPropertyMetadata
    {
      PropertyChangedCallback = (obj, e) =>
      {
        if((bool)e.NewValue && !(bool)e.OldValue & obj is IInputElement)
          ((IInputElement)obj).Focus();
      }
    });
}
于 2010-01-21T08:32:51.520 に答える