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

后端 未结 15 1426
清酒与你
清酒与你 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: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"

    `

        
    

    `

    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"

        
                
                    
            
            
                
                    
                
            
        
    

提交回复
热议问题