作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是创建 UserControl 的新人,现在我正在尝试自定义 UserControl 模板,如下所示:
<UserControl x:Class="WpfApplication1.PieButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="OnLoaded">
<UserControl.Template>
<ControlTemplate>
<Path Name="path" Stroke="Aqua" StrokeThickness="3">
<Path.Fill>
<SolidColorBrush Color="{TemplateBinding Fill}" />
</Path.Fill>
<Path.Data>
......
</UserControl>
public partial class PieButton : UserControl
{
public PieButton()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
}
public Color Fill
{
get { return (Color)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Color), typeof(PieButton));
......
TemplateBinding
在 XAML 中绑定(bind)
Fill
我的 PieButton 的属性来填充路径对象。 Visual Studio 设计器警告我“无法访问或识别 Fill 属性”。
TemplateBinding
从应用此 ControlTemplate 的元素中查找属性名称,应为
PieControl
在这里,但为什么
Fill
属性(property)无法访问这里?
Color="Binding Fill,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}"
最佳答案
根据TemplateBinding to DependencyProperty on a custom control is not working ,
TemplateBinding 不适用于控件上的自定义依赖项属性。
作为解决方案,建议使用{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}
关于wpf - TemplateBinding 如何在 UserControl 模板中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974202/
我是一名优秀的程序员,十分优秀!