Wpf 将边框颜色绑定到ListBox IsFocused属性

Wpf 将边框颜色绑定到ListBox IsFocused属性,wpf,xaml,Wpf,Xaml,如何根据以下代码中的列表框是否具有焦点来更改边框的颜色: <LisBox ItemsSource="{Binding MyCollection}" x:Name="list"/> <Border> <ContentControl Content="{Binding SelectedItem.Content, ElementName=list}"/> </Border> 您可以在边框的样式中使用数据触发器;并将其绑定到ListBox的属性

如何根据以下代码中的列表框是否具有焦点来更改边框的颜色:

<LisBox ItemsSource="{Binding MyCollection}" x:Name="list"/>

<Border>
    <ContentControl Content="{Binding SelectedItem.Content, ElementName=list}"/>
</Border>

您可以在
边框
的样式中使用
数据触发器
;并将其绑定到
ListBox
的属性(在本例中,我使用了
IsKeyboardFocusWithin
)。当
列表框
失去焦点时,它将恢复为原始颜色

    <ListBox ItemsSource="{StaticResource MyCollection}" x:Name="list"/>
    <Border BorderThickness="2">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="BorderBrush">
                    <Setter.Value>
                        <SolidColorBrush Color="Aquamarine" />
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsKeyboardFocusWithin, ElementName=list}" Value="True">
                        <Setter Property="BorderBrush">
                            <Setter.Value>
                                <SolidColorBrush Color="Red" />
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <ContentControl Content="{Binding SelectedItem, ElementName=list}"/>
    </Border>