如何在通用Windows应用程序中绑定模板内的空值?
在你回答之前,看看这篇文章 。
首先得到答案可能会解决以下问题。
这是一个刚刚开始使用Windows 10(Universal Apps)的问题。 在Windows 8.1和其他所有XAML技术中,这种技术完美无瑕。 这是空白通用应用程序项目中的设置:
1.具有附加属性的静态类
创建一个包含Brush
类型的附加属性的类。 把它放在项目的任何地方。
public static class MySettings { public static Brush GetAccentBrush(DependencyObject d) { return (Brush)d.GetValue(AccentBrushProperty); } public static void SetAccentBrush(DependencyObject d, Brush value) { d.SetValue(AccentBrushProperty, value); } public static readonly DependencyProperty AccentBrushProperty = DependencyProperty.RegisterAttached( "AccentBrush", typeof(Brush), typeof(MySettings), null ); }
2.使用附加属性将控件添加到MainPage.xaml
在主页面中,添加一个ContentControl
其中包含一个自定义模板,该模板的背景颜色设置为重点画笔。 重点画笔以风格设置。
如果您现在运行该应用程序,它将显示绿色背景。 一切正常。 但是,如果将值设置为{x:Null}
,则会引发exception。
有人想拍这个吗?
为了记录,这个问题似乎已得到修复。 我的项目引用了通用Windows版本10.0.10240.0。 如原始海报所描述的那样将画笔设置为{x:Null}按预期工作。
您可以添加假附加属性:
public static Brush GetNullAccentBrush(DependencyObject d) { return (Brush)d.GetValue(NullAccentBrushProperty); } public static void SetNullAccentBrush(DependencyObject d, Brush value) { d.SetValue(NullAccentBrushProperty, value); } public static readonly DependencyProperty NullAccentBrushProperty = DependencyProperty.RegisterAttached( "NullAccentBrush", typeof(SolidColorBrush), typeof(MySettings), null );
绑定你的属性setter:
网格背景为空(即使NullAccentBrush上没有真正的绑定…)。
我发现这段代码在资源文件中有效
然后在你的风格中使用该链接到BgNull