6

我有ListBox一个ListBoxItems模板,所以它们包含TextBoxes

带有文本框的列表框

TextBox获得焦点时,我希望ListBoxItem被选中。我发现的一种解决方案如下所示:

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="IsSelected" Value="True"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

这很好用,但是当TextBox失去焦点时,选择也会失去焦点。

有没有办法防止这种情况发生?

4

3 回答 3

15

我发现在没有代码的情况下执行此操作的最佳解决方案是:

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <EventTrigger RoutedEvent="PreviewGotKeyboardFocus">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames
                        Storyboard.TargetProperty="(ListBoxItem.IsSelected)">

                        <DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>
于 2013-03-13T11:02:35.463 回答
0

您还可以将注意力集中在文本框上,但在任何给定时间只选择一个 ListBoxItem,并带有代码。

在列表框 XAML 中:

<ListBox
PreviewLostKeyboardFocus="CheckFocus">
</ListBox>

然后,CheckFocus()在代码隐藏的方法中:

/* Cause the original ListBoxItem to lose focus
    * only if another ListBoxItem is being selected.
    * If a different element type is selected, the
    * original ListBoxItem will keep focus.
    */
private void CheckFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    // check if focus is moving from a ListBoxItem, to a ListBoxItem
    if (e.OldFocus.GetType().Name == "ListBoxItem" && e.NewFocus.GetType().Name == "ListBoxItem")
    {
        // if so, cause the original ListBoxItem to loose focus
        (e.OldFocus as ListBoxItem).IsSelected = false;
    }
}
于 2015-10-16T16:02:03.710 回答
0

从建议的解决方案列表中,没有任何东西可以帮助我解决同样的问题。这是我制作的自定义解决方案:

1)。创建将强制执行焦点的行为(包含附加属性的类):

public class TextBoxBehaviors
{
    public static bool GetEnforceFocus(DependencyObject obj)
    {
        return (bool)obj.GetValue(EnforceFocusProperty);
    }

    public static void SetEnforceFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(EnforceFocusProperty, value);
    }

    // Using a DependencyProperty as the backing store for EnforceFocus.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EnforceFocusProperty =
        DependencyProperty.RegisterAttached("EnforceFocus", typeof(bool), typeof(TextBoxBehaviors), new PropertyMetadata(false,
             (o, e) =>
             {
                 bool newValue = (bool)e.NewValue;
                 if (!newValue) return;

                 TextBox tb = o as TextBox;

                 if (tb == null)
                 {
                     MessageBox.Show("Target object should be typeof TextBox only. Execution has been seased", "TextBoxBehaviors warning",
                       MessageBoxButton.OK, MessageBoxImage.Warning);
                 }

                 tb.TextChanged += OnTextChanged;

             }));

    private static void OnTextChanged(object o, TextChangedEventArgs e)
    {
        TextBox tb = o as TextBox;
        tb.Focus();
       /* You have to place your caret at the end of your text manually, because each focus repalce your caret at the beging of text.*/
        tb.CaretIndex = tb.Text.Length;
    }

}

2)。在 XAML 中使用此行为:

 <DataTemplate x:Key="MyDataTemplate">
    <TextBox behaviors:TextBoxBehaviors.EnforceFocus="True"
             Text="{Binding Path=MyProperty, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
于 2016-06-21T10:48:17.057 回答