如何在ComboBox获得焦点时改变边框背景

时间:2014-08-15 20:39:41

标签: c# wpf combobox

我有一个内容组合框的边框,现在我想在ComboBox获得焦点时更改边框的背景!

我尝试过这段代码,但它不起作用:(

<Border CornerRadius="4" Margin="6,2,2,2" >
    <StackPanel Orientation="Horizontal" x:Name="Tester">   
        <Label Content=" Signature :" Margin="10,0,0,0" FontFamily="Times New Roman" FontWeight="Normal" FontSize="14"  ToolTip="You can join a file text containing your query with simple drag and drop of file here"/>
        <ComboBox x:Name="Signatures" Margin="2,4,4,4" IsSynchronizedWithCurrentItem="False" FontWeight="Normal" FontFamily="Times New Roman" Width="180" IsTextSearchEnabled="True" IsEditable="True" FontSize="14" MouseDoubleClick="Signatures_MouseDoubleClick" MouseLeftButtonDown="Signatures_MouseDoubleClick"  >
            <ComboBoxItem Content="Serializable" HorizontalAlignment="Left" FontWeight="Normal" />
            <ComboBoxItem Content="Comparable" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="CharSequence" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="Number" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="String" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="long" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="short" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="integer" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="double" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="float" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="byte" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="boolean" HorizontalAlignment="Left"/>
            <ComboBoxItem Content="void" HorizontalAlignment="Left"/>
        </ComboBox>
    </StackPanel>
    <Border.Style>
        <Style TargetType="Border">
            <Setter Property="Background" Value="{x:Null}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Signatures, Path=IsFocused}" Value="True">
                    <Setter Property="Background" Value="BurlyWood"/>
                </DataTrigger>

            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

问题在于焦点不完全在你的ComboBox上,而是在子元素上,而AFAIK WPF无法告诉你焦点是否在一个元素内。

所以你应该添加更多DataTrigger s:

<Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=Signatures,Path=IsFocused}" Value="True">
        <Setter Property="Background" Value="BurlyWood"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=Signatures,Path=IsSelectionBoxHighlighted}" Value="True">
        <Setter Property="Background" Value="BurlyWood"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=Signatures,Path=IsDropDownOpen}" Value="True">
        <Setter Property="Background" Value="BurlyWood"/>
    </DataTrigger>
相关问题