[英]How can I use a Custom Property within a Custom Template?
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
. 因此,我启动Blend并在表面上放置一个按钮,然后
Edit a Copy of the Template
。 I now have my custom template, and I put a small little Ellipse
inside it: 现在,我有了自定义模板,并在其中放了一个小的
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>
... ...
I right click this button and select Make into UserControl
named MyButton
. 我右键单击此按钮,然后选择“
Make into UserControl
名为MyButton
Make into UserControl
。 In the code behind for this button I add a custom dependency property for the
Brush`: 在此按钮后面的代码中,我
dependency property for the
Brush`添加了一个自定义dependency property for the
:
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? 如何将我的椭圆颜色(上面的x:Name =“ ellipse”)
MyColor
到MyColor
属性?
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
. 编辑:所以我使用
Fill={TemplateBinding MyColor}
绑定用户控件中的Fill属性,但是随后我得到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? 好的,所以我需要将模板定位到自定义MyButton类型,但是我该怎么做呢? I can simply change Button to MyButton, because then I get
Property 'ContentTemplate' was not found in type '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>
Okay, first of all, your MyButton
control should have a DependencyProperty, say, MyColor
-- do you have it? 好的,首先,您的
MyButton
控件应该具有DependencyProperty,例如MyColor
-您有吗? In your template, you should bind to that property using TemplateBinding
. 在模板中,应该使用
TemplateBinding
绑定到该属性。 In the XAML using your control you use it just as you are doing: <local:MyButton Width="130" MyColor="Red"/>
在XAML中,您可以像使用控件一样使用它:
<local:MyButton Width="130" MyColor="Red"/>
It seems that your template is missing the template binding: 您的模板似乎缺少模板绑定:
<Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}"...>
<Grid Background="{TemplateBinding Background}" Margin="1">
...
<Ellipse x:Name="ellipse" Width="10" Height="10" HorizontalAlignment="Left"
Fill="{TemplateBinding MyColor}" />
</Grid>
</Border>
By the way, MyColor
should be really a Brush
, not just a Color
. 顺便说一句,
MyColor
应该实际上是一个Brush
,而不仅仅是Color
。
The problem here is that UserControl
is not the right basis for your control. 这里的问题是
UserControl
不是UserControl
的正确基础。 You ought to be creating a Templated control. 您应该创建一个模板控件。
In Visual Studio add new "Silverlight Templated Control" to your project called MyButton
. 在Visual Studio中,将新的“ Silverlight模板控件”添加到名为
MyButton
的项目中。
Open MyButton.cs and change its base class to Button
. 打开MyButton.cs并将其基类更改为
Button
。 Drop into the code your MyColor
dependency property. 将您的
MyColor
依赖项属性放入代码中。
Open Themes/Generic.xaml and find the default style that has been placed there for this new MyButton
control. 打开Themes / Generic.xaml并找到该新
MyButton
控件放置在其中的默认样式。 Replace the Template
in that style with your template. 更换
Template
与您的模板风格。 Now the Fill="{TemplateBinding MyColor}"
in your template will work. 现在,您模板中的
Fill="{TemplateBinding MyColor}"
将可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.