我正在尝试一些我认为非常简单的事情。每当键盘焦点位于网格内时,我都希望在网格内显示边框。
但是绑定无法正常工作,我无法发现问题所在。我希望有人能发现我出了错。
我创建了一个简单的测试应用程序来说明问题。这是窗口的内容
<StackPanel>
<!-- This text box is just so that something else can have focus at times -->
<TextBox Width="200" Height="20"/>
<!--
This grid should show its border whenever keyboard focus is within
I even have an event handler dumping out the new value of when it changes.
-->
<Grid x:Name="DetailGrid"
Width="400" Height="400" Focusable="True"
IsKeyboardFocusWithinChanged="DetailGrid_OnIsKeyboardFocusWithinChanged">
<Border BorderBrush="White" BorderThickness="0" >
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocusWithin,ElementName=DetailGrid}" Value="True">
<Setter Property="BorderThickness" Value="3"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<!-- This text box is just so that the grid can get keyboard focus within -->
<TextBox Width="200" Height="20" VerticalAlignment="Center"/>
</Border>
</Grid>
</StackPanel>
还有代码隐藏事件处理程序,该事件处理程序确认属性已更改为我所期望的
private void DetailGrid_OnIsKeyboardFocusWithinChanged(object sender,DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("DetailGrid keyboard focus within = " + (bool) e.NewValue) ;
}
- “ DetailGrid”在文本框周围包含一个零厚度的边框。
- 边界上的触发器绑定到的IsKeyboardFocusWithin 父DetailGrid。
- 如果用户单击文本框,则网格的IsKeyboardFocusWithin属性将设置为true(我已经在后面的代码中对此进行了验证)。
- 触发器说,当DetailGrid的IsKeyboardFocusWithin属性设置为true时,边框厚度应增加为3。
但是那没有发生。
我尝试更改“调试输出”窗口设置,使其对WPF绑定的详细程度更高,但在那里看不到任何问题。
我尝试将绑定更改为使用FindAncestor,但这没什么区别。
我的直觉告诉我这必须是非常简单和愚蠢的,但是我看不到。我在这里想念什么?