1 回答

TA貢獻(xiàn)1801條經(jīng)驗 獲得超16個贊
應(yīng)用于 a 的模板中的元素TextBox不能綁定到 a 的屬性MyClass,除非有一個MyClass元素可以綁定到可視化樹中的某處。
如果您希望能夠設(shè)置 a 的自定義HasFocus屬性TextBox,您應(yīng)該創(chuàng)建一個附加屬性:
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元素設(shè)置:
<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 關(guān)注
- 135 瀏覽
添加回答
舉報