如何绑定到通用 Windows 应用程序模板内的空值?

How can I bind to a null value inside a template in a Universal Windows App?


回答之前先看.

首先得到答案可能会解决以下问题。


这是一个刚从 Windows 10(通用应用程序)开始的问题。在 Windows 8.1 和所有其他 XAML 技术中,此技术完美无缺。这是空白通用应用程序项目中的设置:

1.静态 class 附带 属性

创建一个 class,其中包含 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>

有人愿意试一试吗?

您可以添加假附件 属性 :

    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 :

<Setter Property="local:MySettings.AccentBrush"
        Value="{Binding local:MySettings.NullAccentBrush}" />

网格背景为空(即使在 NullAccentBrush 上没有真正的绑定...)。

郑重声明,此问题似乎已得到解决。我的项目引用了 Universal Windows 版本 10.0.10240.0。按照原始海报的描述将画笔设置为 {x:Null} 按预期工作。

我发现此代码在资源文件中有效

<Color x:Key="BgNull"></Color>

然后按照您的风格使用 link 到 BgNull

<Setter Property="Background" Value="{StaticResource BgNull}"/>