1 回答
TA贡献1801条经验 获得超16个赞
应用于 a 的模板中的元素TextBox不能绑定到 a 的属性MyClass,除非有一个MyClass元素可以绑定到可视化树中的某处。
如果您希望能够设置 a 的自定义HasFocus属性TextBox,您应该创建一个附加属性:
public class FocusExtensions
{
public static readonly DependencyProperty SetHasFocusProperty = DependencyProperty.RegisterAttached(
"HasFocus",
typeof(bool),
typeof(FocusExtensions),
new FrameworkPropertyMetadata(false)
);
public static void SetHasFocus(TextBox element, bool value)
{
element.SetValue(SetHasFocusProperty, value);
}
public static bool GetHasFocus(TextBox element)
{
return (bool)element.GetValue(SetHasFocusProperty);
}
}
它可以为任何TextBox元素设置:
<TextBox local:FocusExtensions.HasFocus="True">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="local:FocusExtensions.HasFocus" Value="True">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
- 1 回答
- 0 关注
- 143 浏览
添加回答
举报
