I used similarly to Robert's solution, but without code (using the attached behavior).
To do this,
Firstly. Create a separate FocusBehaviour class:
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 } }
Secondly. Add a style to the resources section (my style is rounded in the center). Setter notification for the FocusBehaviour.IsListBoxItemSelected property. You must 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"/> <Setter Property="Foreground" Value="White"/> <Setter Property="behave:FocusBehaviour.IsListBoxItemSelected" Value="True"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
`
Thirdly. (optional, for the inverse problem)
You will encounter, if not any, inverse problem - focus on the TextBox when choosing ListBoxItem. I recommend using another property of the Behavior class, IsFocused. Here is an example template for ListBoxItem , pay attention to 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>