Metro XAML – TemplateBinding和SolidColorBrush的问题
这是一个简单的自定义控件来说明我的问题
public sealed class TestControl : Control { public static DependencyProperty TestColorProperty = DependencyProperty.Register("TestColor",typeof(Brush),typeof(TestControl),new PropertyMetadata(new SolidColorBrush(Colors.Blue))); public Brush TestColor { get { return (Brush)GetValue(TestColorProperty); } set { SetValue(TestColorProperty,value); } } public TestControl() { this.DefaultStyleKey = typeof(TestControl); } } 如您所见,它具有单个Brush依赖项属性,默认值为Blue(在PropertyMetaData中设置,如上所示). 这是我在Generic.xaml中控制的XAML <Style TargetType="local:TestControl"> <Setter Property="TestColor" Value="Red" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:TestControl"> <Border Background="{TemplateBinding TestColor}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock Text="TEST" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 如您所见,我在Style setter中将TestColor Brush依赖项属性设置为Red – 覆盖我在PropertyMetaData中声明的默认值Blue. 那么你认为边框背景设置的是什么颜色?红色还是蓝色? 答案都不是. 如果我在我的控件中设置一个断点,此值应该可用(例如OnApplyTemplate作为示例),那么值为null,而不是预期的Red(默认值).实际上,我在控件的所有生命周期点都设置了断点,并且从不使用ProprtyMetaData中的默认值. 在样式中设置值也没有任何作用(根据我的样式设置器delaration,它没有设置为Blue.这表明样式设置器以某种方式失败了SolidColorBrush. 但是,这有效 public BlankPage() { this.InitializeComponent(); testcont.TestColor = new SolidColorBrush(Colors.Orange); } 这也有效: <Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> <local:TestControl TestColor="Green" /> </Grid> 但是TemplateBinding不起作用,这很重要,因为我试图编写可重用的自定义控件. 这是一个错误吗? 院长 解决方法
这是我们要解决的问题.同时,将其设置为:
<SolidColorBrush x:Key="DefaultTestColorBrush">Red</SolidColorBrush> 然后在你的模板中: <Setter Property="TestColor" Value="{StaticResource DefaultTestColorBrush}" /> 那么你现在应该能够清除这个障碍. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用Windows系统cmd运行Python时中文乱码问题有效的解决方式
- 我应该使用哪种Windows ext3驱动程序?
- windows-8 – 启动另一个Windows应用商店应用
- windows-7 – 具有内部端口重定向的Windows 7,有可能吗?
- .net – 请描述一下使用Microsoft C/C++LI的经历
- windows-server-2008-r2 – PowerShell脚本的电子邮件结果
- 从Windows批处理文件上传文件到FTP时,“无法打开到端口xxxx
- .net – 内部可重用库 – 重用为dll或项目?
- 调试 – 当ntdll.dll映射到新进程时如何进入WinDBG
- 在没有安装JRE的Windows中运行一个java程序作为exe