在回答之前,请先查看 this post .
首先得到答案可能会解决以下问题。
这是一个刚从 Windows 10(通用应用程序)开始的问题。在 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
,该模板具有一个 Grid
,其背景颜色设置为强调画笔。强调画笔设置为样式。
<Page x:Class="UniversalTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:UniversalTest"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="MyControl"
TargetType="ContentControl">
<Setter Property="local:MySettings.AccentBrush"
Value="Green" /> <!-- Setting value here -->
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ContentControl Style="{StaticResource MyControl}">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid Background="{TemplateBinding local:MySettings.AccentBrush}"> <!-- Using value here -->
<TextBlock Text="Howdy World!" />
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
</Grid>
</Page>
如果您现在运行该应用程序,它将以绿色背景显示。一切正常。但是,如果您将该值设置为 {x:Null}
,则会引发异常。
<Page.Resources>
<Style x:Key="MyControl"
TargetType="ContentControl">
<Setter Property="local:MySettings.AccentBrush"
Value="{x:Null}" /> <!-- Null value here -->
</Style>
</Page.Resources>
有人愿意试一试吗?
最佳答案
郑重声明,此问题似乎已得到解决。我的项目引用了通用 Windows 版本 10.0.10240.0。按照原始海报的描述将画笔设置为 {x:Null} 按预期工作。
关于c# - 如何绑定(bind)到通用 Windows 应用程序模板内的空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32818652/