绑定到WPF父网格面板';s属性失败,但不生成绑定警告消息
我在尝试一些我认为很简单的事情。每当键盘焦点位于网格内时,我希望使网格内的边框出现 但是装订不好,我看不出有什么问题。我希望有人能发现我哪里做错了 我创建了一个简单的测试应用程序来说明这个问题。这是窗口的内容绑定到WPF父网格面板';s属性失败,但不生成绑定警告消息,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我在尝试一些我认为很简单的事情。每当键盘焦点位于网格内时,我希望使网格内的边框出现 但是装订不好,我看不出有什么问题。我希望有人能发现我哪里做错了 我创建了一个简单的测试应用程序来说明这个问题。这是窗口的内容 <StackPanel> <!-- This text box is just so that something else can have focus at times --> <TextBox Width="200" Height="20
<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>
- “DetailGrid”在文本框周围包含一个零厚度的边框李>
- 边框上的触发器绑定到 父详细信息网格
- 如果用户单击文本框,网格的IsKeyboardFocusWithin属性将设置为true(我已经在代码隐藏中对此进行了验证)
- 触发器表示,当DetailGrid的IsKeyboardFocusInner属性设置为true时,边框厚度应增加到3
我的直觉告诉我这一定是非常简单和愚蠢的事情,但我看不出来。我在这里遗漏了什么?您应该将XAML中的
边框改为:
我从Border
定义中删除了BorderThickness=“0”
标记,因为它的值大于通过样式设置的值,因此您的设置器无法覆盖它。如您所见,我将此默认值移动到了默认的样式
Setter
我还将边框笔刷
更改为黑色
,以便您可以看到它变得可见。您应该将XAML中的边框
改为:
我从Border
定义中删除了BorderThickness=“0”
标记,因为它的值大于通过样式设置的值,因此您的设置器无法覆盖它。如您所见,我将此默认值移动到了默认的样式
Setter
我还将BorderBrush
更改为Black
,以便您可以看到它变得可见。Grrrr。我知道这会让我觉得自己很傻。就在我面前。非常感谢你的帮助。我知道这会让我觉得自己很傻。就在我面前。非常感谢你的帮助。
private void DetailGrid_OnIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("DetailGrid keyboard focus within = " + (bool) e.NewValue) ;
}