Selecting a Textbox Item in a Listbox does not change the selected item of the listbox

后端 未结 15 1339
清酒与你
清酒与你 2020-11-27 04:10

I Have a wpf Listbox that display\'s a list of textboxes. When I click on the Textbox the Listbox selection does not change. I have to click next to the TextBox to select th

相关标签:
15条回答
  • 2020-11-27 04:58

    Is there some property I need to set for the Textbox to forward the click event to the Listbox?

    It's not a simple property, but you can handle the GotFocus event on your TextBox, then use VisualTreeHelper to find the ListBoxItem and select it:

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox myTextBox = sender as TextBox;
        DependencyObject parent = VisualTreeHelper.GetParent(myTextBox);
        while (!(parent is ListBoxItem))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        ListBoxItem myListBoxItem = parent as ListBoxItem;
        myListBoxItem.IsSelected = true;
    }
    
    0 讨论(0)
  • 2020-11-27 04:59

    I used similar to Robert's solution, but without code behind (using attached behavior).

    To do so,

    First. Create separate class FocusBehaviour:

    
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace MyBehaviours
    {
        public class FocusBehaviour
        {
            #region IsFocused
            public static bool GetIsFocused(Control control)
            {
                return (bool) control.GetValue(IsFocusedProperty);
            }
    
            public static void SetIsFocused(Control control, bool value)
            {
                control.SetValue(IsFocusedProperty, value);
            }
    
            public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.RegisterAttached(
                "IsFocused", 
                typeof(bool),
                typeof(FocusBehaviour), 
                new UIPropertyMetadata(false, IsFocusedPropertyChanged));
    
            public static void IsFocusedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                var control = sender as Control;
                if (control == null || !(e.NewValue is bool))
                    return;
                if ((bool)e.NewValue && !(bool)e.OldValue)
                    control.Focus();
            }
    
            #endregion IsFocused
    
            #region IsListBoxItemSelected
    
            public static bool GetIsListBoxItemSelected(Control control)
            {
                return (bool) control.GetValue(IsListBoxItemSelectedProperty);
            }
    
            public static void SetIsListBoxItemSelected(Control control, bool value)
            {
                control.SetValue(IsListBoxItemSelectedProperty, value);
            }
    
            public static readonly DependencyProperty IsListBoxItemSelectedProperty = DependencyProperty.RegisterAttached(
                "IsListBoxItemSelected", 
                typeof(bool),
                typeof(FocusBehaviour), 
                new UIPropertyMetadata(false, IsListBoxItemSelectedPropertyChanged));
    
            public static void IsListBoxItemSelectedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                var control = sender as Control;
                DependencyObject p = control;
                while (p != null && !(p is ListBoxItem))
                {
                    p = VisualTreeHelper.GetParent(p);
                } 
    
                if (p == null)
                    return;
    
                ((ListBoxItem)p).IsSelected = (bool)e.NewValue;
            }
    
            #endregion IsListBoxItemSelected
        }
    }
    

    Second. Add a style in resources section (my style is rounded black on focus). Notice setter for FocusBehaviour.IsListBoxItemSelected property. You should reference it in xmlns:behave="clr-namespace:MyBehaviours"

    `

        <Style x:Key="PreviewTextBox" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border
                            Margin="6,2,0,4"
                            BorderBrush="#FFBDBEBD"
                            BorderThickness="1"
                            CornerRadius="8"
                            Background="White"
                            VerticalAlignment="Stretch"
                            HorizontalAlignment="Stretch"
                            MinWidth="100"
                            x:Name="bg">
                            <ScrollViewer 
                                x:Name="PART_ContentHost" 
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                <Setter Property="Background" TargetName="bg" Value="Black"/>
                                <Setter Property="Background" Value="Black"/><!-- we need it for caret, it is black on black elsewise -->
                                <Setter Property="Foreground" Value="White"/>
                                <Setter Property="behave:FocusBehaviour.IsListBoxItemSelected" Value="True"/>
                            </Trigger>
    
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    `

    Third. (optional, for reverse task)

    You will meet, if not any, reverse task - focusing on TextBox when ListBoxItem get selected. I recommend using another property of Behaviour class, IsFocused. Here is a sample template for ListBoxItem, please notice Property="behave:FocusBehaviour.IsFocused" and FocusManager.IsFocusScope="True"

        <DataTemplate x:Key="YourKey" DataType="{x:Type YourType}">
                <Border
                Background="#FFF7F3F7"
                BorderBrush="#FFBDBEBD"
                BorderThickness="0,0,0,1"
                FocusManager.IsFocusScope="True"
                x:Name="bd"
                MinHeight="40">
                    <TextBox
                        x:Name="textBox"
                        Style="{StaticResource PreviewTextBox}"
                        Text="{Binding Value}" />
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger
                    Binding="{Binding IsSelected,RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                    Value="True">
                    <Setter
                        TargetName="textBox"
                        Property="behave:FocusBehaviour.IsFocused" 
                        Value="True" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    
    0 讨论(0)
  • 2020-11-27 05:01

    Old discussion, but maybe my answer helps others....

    Ben's solution has the same problem as Grazer's solution. The bad thing is that the selection depends on the [keyboard] focus of the textbox. If you have another control on your dialog (i.e. a button) the focus gets lost when clicking the button and the listboxitem becomes un-selected (SelectedItem == null). So you have different behavior for clicking the item (outside the textbox) and clicking in the textbox. This is very tedious to handle and looks very strange.

    I'm quite sure there is no pure XAML solution for this. We need code-behind for this. The solution is close to what Mark suggested.

    (in my example I use ListViewItem instead of ListBoxItem, but the solution works for both).

    Code-behind:

    private void Element_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var frameworkElement = sender as FrameworkElement;
            if (frameworkElement != null)
            {
                var item = FindParent<ListViewItem>(frameworkElement);
                if (item != null)
                    item.IsSelected = true;
            }
        }
    

    with FindParent (taken from http://www.infragistics.com/community/blogs/blagunas/archive/2013/05/29/find-the-parent-control-of-a-specific-type-in-wpf-and-silverlight.aspx ):

    public static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            //get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);
    
            //we've reached the end of the tree
            if (parentObject == null) return null;
    
            //check if the parent matches the type we're looking for
            T parent = parentObject as T;
            if (parent != null)
                return parent;
    
            return FindParent<T>(parentObject);
        }
    

    In my DataTemplate:

    <TextBox Text="{Binding Name}"
            PreviewMouseDown="Element_PreviewMouseDown"/>
    
    0 讨论(0)
  • 2020-11-27 05:01

    The following is a simplification of @Ben's answer without having to override the DataTemplate. It can even be applied as a static style. Tested with a ListView containing a GridView > GridViewColumn > TextBox.

    Example:

    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsSelected" Value="True"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.Resources>
    
    0 讨论(0)
  • 2020-11-27 05:03

    Be sure to use appropriate TargetType: ListViewItem, ListBoxItem or TreeViewItem.

    <Style TargetType="ListViewItem">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="true">
                <Setter Property="IsSelected" Value="true" />
            </Trigger>
        </Style.Triggers>
    </Style>
    
    0 讨论(0)
  • 2020-11-27 05:04

    I use a class handler to set this behavior. Doing it this way will fix all of the list views in the application. I don't know why this is not the default behavior.

    In your App.xaml.cs, add the following to OnStartup:

    protected override void OnStartup(StartupEventArgs e)
        {
            EventManager.RegisterClassHandler(typeof (ListViewItem), 
                                              ListViewItem.PreviewGotKeyboardFocusEvent,
                                              new RoutedEventHandler((x,_) => (x as ListViewItem).IsSelected = true));
        }
    
    0 讨论(0)
提交回复
热议问题