如何在WPF中突出显示基于键盘焦点的用户控件?

时间:2015-01-22 09:20:49

标签: c# wpf

我设计的形式就像这样的ini WPF:

usercontrol wpf

那么当我将键盘聚焦在Halaman12b的某个文本框中时,如何在每列中以Halaman12突出显示不同颜色的背景?

我使用了Hal 12的用户控件,如下所示:

 <UserControl x:Class="Susenas2015.Content.KOR.Halaman12"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:kor="clr-namespace:Susenas2015.Content.KOR"
         mc:Ignorable="d" 
         d:DesignHeight="1200" d:DesignWidth="1500">
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
    <StackPanel Orientation="Horizontal">
        <kor:Halaman12a Width="300"></kor:Halaman12a>
        <ItemsControl ItemsSource="{Binding ListART5_1}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <kor:Halaman12b DataContext="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</ScrollViewer>

所以当我在Halaman12的一个TextBox中获得焦点时,我想要突出显示特定的列?

由于

1 个答案:

答案 0 :(得分:2)

假设kor:Halaman12b UserControl没有将Background设置为本地值,您可以根据IsKeyboardFocusWithin更改控件背景

<kor:Halaman12b DataContext="{Binding}">
    <kor:Halaman12b.Style>
        <Style TargetType="{x:Type kor:Halaman12b}">
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </kor:Halaman12b.Style>
</kor:Halaman12b>
相关问题