【发布时间】:2016-08-14 16:40:49
【问题描述】:
我有一个用 XAML 编写的相当简单的自定义控件,但我在编写一些依赖属性以便更改颜色时遇到了一些麻烦。
这是我们感兴趣的控件。
<Viewbox>
<Path Name="shape" Fill="Gray" Data="abc"/>
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="shape" Property="Fill" Value="Gold" />
</Trigger>
</ControlTemplate.Triggers>
我想要实现的是将此处的填充(灰色和金色)替换为我可以更改为使用控件的属性。
所以我可以结束这个
<MyControl BackColor="Blue" ForeColor="Red" />
我尝试了一些不同的视频,包括 MVA 视频,但我不确定我哪里出错了。
我已经尝试过这个依赖属性。
public Color BackgroundColor
{
get { return (Color)GetValue(BackgroundColorProperty); }
set { SetValue(BackgroundColorProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundColorProperty =
DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(StarRatingControl), new PropertyMetadata(Color.FromRgb(0,0,0)));
然后使用
Fill="{TemplateBinding BackgroundColor}"
但我只是收到以下错误:
“在类型‘ToggleButton’上找不到静态成员‘BackgroundColorProperty’。”
和
“成员“BackgroundColor”无法识别或无法访问。”
这两个都在 XAML 文件中,而不是 CS 文件中。
请有人帮助/解释我哪里出错了。
【问题讨论】:
-
请提供更多代码。我可以想象什么不起作用,但结果是猜测
-
请注意,属性类型应该是 Brush,而不是 Color。