如何在自定义模板中使用自定义属性?
假设想要创建一个左侧有一个小椭圆的自定义按钮。 我希望这个椭圆的颜色是可数据绑定的。
因此,我启动 Blend 并在表面上放置一个按钮,然后编辑模板的副本
。 我现在有了自定义模板,并在其中放置了一个小 Ellipse
:
我
<Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="White" CornerRadius="3">
<Grid Background="{TemplateBinding Background}" Margin="1">
<snip />
<!-- My ellipse here -->
<Ellipse x:Name="ellipse" Width="10" Height="10" HorizontalAlignment="Left" />
</Grid>
</Border>
……
右键单击此按钮并选择名为 Make into UserControl
>我的按钮。 在此按钮的后面代码中,我为 Brush` 添加了一个自定义依赖属性:
public Brush MyColor
{
get { return (Brush)GetValue(MyColorProperty); }
set { SetValue(MyColorProperty, value); }
}
public static readonly DependencyProperty MyColorProperty = DependencyProperty.Register("MyColor", typeof(Brush), typeof(MyButton), new PropertyMetadata(new opertyChangedCallback(MyColorPropertyChanged)));
private static void MyColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var thisControl = d as MyButton;
}
然后我可以使用我的自定义按钮:
<local:MyButton Width="130" MyColor="Red"/>
但随后我就卡住了。如何将椭圆颜色(上面的 x:Name="ellipse")绑定到我的 MyColor
属性?
编辑:因此,我使用 Fill={TemplateBinding MyColor}
绑定用户控件中的 Fill 属性,但随后我得到 Property MyColor was not found in type Button
。 好的,所以我需要将我的模板定位到自定义 MyButton 类型,但我该怎么做呢? 我可以简单地将 Button 更改为 MyButton,因为然后我得到 Property 'ContentTemplate' was not found in type 'MyButton'
<UserControl
<snip/>
x:Class="SilverlightApplication1.MyButton">
<UserControl.Resources>
<Style x:Key="ButtonStyle1" TargetType="Button">
<snip/>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Button Content="Test Button" Style="{StaticResource ButtonStyle1}"/>
</Grid>
</UserControl>
Let's say a want to create a custom button that has a small ellipse on the left hand side.
I want the color of this ellipse to be data bindable.
So I fire up Blend and put a button on the surface and Edit a Copy of the Template
.
I now have my custom template, and I put a small little Ellipse
inside it:
...
<Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="White" CornerRadius="3">
<Grid Background="{TemplateBinding Background}" Margin="1">
<snip />
<!-- My ellipse here -->
<Ellipse x:Name="ellipse" Width="10" Height="10" HorizontalAlignment="Left" />
</Grid>
</Border>
...
I right click this button and select Make into UserControl
named MyButton
.
In the code behind for this button I add a custom dependency property for the
Brush`:
public Brush MyColor
{
get { return (Brush)GetValue(MyColorProperty); }
set { SetValue(MyColorProperty, value); }
}
public static readonly DependencyProperty MyColorProperty = DependencyProperty.Register("MyColor", typeof(Brush), typeof(MyButton), new PropertyMetadata(new opertyChangedCallback(MyColorPropertyChanged)));
private static void MyColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var thisControl = d as MyButton;
}
I can then use my custom button:
<local:MyButton Width="130" MyColor="Red"/>
But then I'm stuck. How do I bind my Ellipse color (x:Name="ellipse" above) to my MyColor
property?
EDIT: So I bind the Fill property in the user control using Fill={TemplateBinding MyColor}
, but then I get Property MyColor was not found in type Button
.
Ok, so I need to target my template to the custom MyButton type, but how can I do that?
I can simply change Button to MyButton, because then I get Property 'ContentTemplate' was not found in type 'MyButton'
<UserControl
<snip/>
x:Class="SilverlightApplication1.MyButton">
<UserControl.Resources>
<Style x:Key="ButtonStyle1" TargetType="Button">
<snip/>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Button Content="Test Button" Style="{StaticResource ButtonStyle1}"/>
</Grid>
</UserControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是
UserControl
不是您的控件的正确基础。您应该创建一个模板化控件。在 Visual Studio 中,将新的“Silverlight 模板化控件”添加到名为
MyButton
的项目中。打开 MyButton.cs 并将其基类更改为
Button
。将您的MyColor
依赖属性放入代码中。打开 Themes/Generic.xaml 并找到为这个新的
MyButton
控件放置在那里的默认样式。将该样式中的Template
替换为您的模板。现在模板中的Fill="{TemplateBinding MyColor}"
就可以工作了。The problem here is that
UserControl
is not the right basis for your control. You ought to be creating a Templated control.In Visual Studio add new "Silverlight Templated Control" to your project called
MyButton
.Open MyButton.cs and change its base class to
Button
. Drop into the code yourMyColor
dependency property.Open Themes/Generic.xaml and find the default style that has been placed there for this new
MyButton
control. Replace theTemplate
in that style with your template. Now theFill="{TemplateBinding MyColor}"
in your template will work.好的,首先,您的
MyButton
控件应该有一个 DependencyProperty,例如MyColor
——您有吗?在您的模板中,您应该使用TemplateBinding
绑定到该属性。在使用控件的 XAML 中,您可以按照如下方式使用它:看来您的模板缺少模板绑定:
顺便说一下,
MyColor
应该是一个Brush
,而不仅仅是一个Color
。Okay, first of all, your
MyButton
control should have a DependencyProperty, say,MyColor
-- do you have it? In your template, you should bind to that property usingTemplateBinding
. In the XAML using your control you use it just as you are doing:<local:MyButton Width="130" MyColor="Red"/>
It seems that your template is missing the template binding:
By the way,
MyColor
should be really aBrush
, not just aColor
.