我怎样才能绑定到一个通用的Windows应用程序模板内的空值? [英] How can I bind to a null value inside a template in a Universal Windows App?
问题描述
在你回答之前,看的这个帖子。
Before you answer, look at this post.
的获得一个回答首先可以解决以下问题。的
这是刚开始使用Windows 10有问题(通用应用程序)。在Windows 8.1,和所有其他XAML技术,这种技术完美地工作。下面是一片空白通用的应用程序项目的设置:
This is a problem that just started with Windows 10 (Universal Apps). In Windows 8.1, and every other XAML technology, this technique worked flawlessly. Here's the setup in a blank universal app project:
1。静态类附加属性
创建容纳类型的附加属性的类刷
。把项目中的这个任意位置。
Create a class that houses an attached property of type Brush
. Put this anywhere in the project.
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中
与具有电网
与背景颜色设置为口音刷自定义模板。该口音刷设置在样式
In the main page, add a ContentControl
with a custom template that has a Grid
with its background color set to the accent brush. The accent brush is set in a style.
<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:空}
,它抛出一个异常
If you run the app now, it will show up with a green background. Everything works. However, if you set the value as {x:Null}
, it throws an exception.
<Page.Resources>
<Style x:Key="MyControl"
TargetType="ContentControl">
<Setter Property="local:MySettings.AccentBrush"
Value="{x:Null}" /> <!-- Null value here -->
</Style>
</Page.Resources>
有没有人愿意采取射击在此?
Would anyone like to take a shot at this?
推荐答案
有关记录,这个问题似乎已经得到修复。我的项目引用通用Windows版本10.0.10240.0。刷设置为{X:空}。由原始的海报按预期工作。
For the record, this issue appears to have been fixed. My project references Universal Windows version 10.0.10240.0. Setting the brush to {x:Null} as described by the original poster works as expected.
这篇关于我怎样才能绑定到一个通用的Windows应用程序模板内的空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!