这是我关于stackoverflow的第一篇文章。
我制作了自己的组合框,该组合框继承自combobox。我仅在可编辑模式下使用它。
当组合框具有键盘焦点时,我想将边框涂成蓝色,就像在文本框上看到的那样。
我有一个完美的XAML解决方案,但是我想在代码隐藏中做到这一点,因为我将组合框放置在DLL中,并且ressoucefiles和DLL有问题。
以下XAML起作用。但是我想要在代码隐藏中。
<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot" snapsToDevicePixels="true" Background="{StaticResource ComboBox.Static.Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{StaticResource ComboBox.Static.Border}">
<Border x:Name="splitBorder" Width="{Dynamicresource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" snapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
<Path x:Name="arrow" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>
</Border>
</Border>
<ControlTemplate.Triggers>
<!--This paint the border blue -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsKeyboardFocusWithin,RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
<Condition Binding="{Binding IsEditable,RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}" Value="true"/>
</MultiDataTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource ComboBox.MouseOver.Border}"/>
</MultiDataTrigger>
<!--This paint the border blue -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
我尝试过:
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
var parent = (Border)editableTextBox.Parent;
parent.BorderBrush = brushes.Blue;
parent.BorderThickness = new Thickness(3);
base.OnGotKeyboardFocus(e);
}
这不起作用。它正在绘制内部文本框的边框。
我也尝试过:
var template = this.Template;
var myControl = template.findname("templateRoot",this);
Debug.Print(myControl.GetType().ToString());
它打印:“ System.Windows.Controls.Grid”,告诉templateRoot是一个Grid。因此没有边界。
但是,如果我正确阅读XAML,则表示templateRoot是边框。
我在做什么错?我找到的是错误的templateRoot?