2011-10-14 125 views
4

我们假设想创建一个左侧有一个小椭圆的自定义按钮。 我想这个椭圆的颜色是数据可绑定的。如何在自定义模板中使用自定义属性?

所以我启动了Blend并在表面上放了一个按钮,并且Edit a Copy of the Template。 现在我有我的自定义模板,我把一个小的小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命名MyButton。 在后面的代码为这个按钮我添加自定义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; 
} 

然后我就可以用我的自定义按钮:

<local:MyButton Width="130" MyColor="Red"/> 

但后来我卡。如何将我的Ellipse颜色(上面的x:Name =“ellipse”)绑定到我的MyColor属性?

编辑:所以我绑定用户控件中的填充属性使用Fill={TemplateBinding MyColor},但后来我得到Property MyColor was not found in type Button。 好的,所以我需要将我的模板定位到自定义MyButton类型,但我该怎么做? 我可以简单地改变按钮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> 
+0

WPF? Asp.Net? ... – IAbstract

+0

@IAbstract,XAML排除了ASP.NET,不是吗?我们剩下的就是WPF和/或Silverlight。我认为上下文是WPF/Silverlight不可知的,因此我的XAML标签。 –

+0

重要的是要有完整的标签......有人可能正在寻找相同的解决方案,但使用WPF或Silverlight代替。我对Asp.Net并不熟悉 - 所以请原谅我的无知。我认为提供问题适用的所有标签很重要。 :) – IAbstract

回答

1

这里的问题是UserControl不是您控制的正确基础。你应该创建一个模板控件。

在Visual Studio中为您的项目添加新的“Silverlight模板控制”,名为MyButton

打开MyButton.cs并将其基类更改为Button。放入代码中您的MyColor依赖项属性。

打开主题/ Generic.xaml并找到已放置在那里的新的MyButton控件的默认样式。用你的模板替换那种风格的Template。现在您的模板中的Fill="{TemplateBinding MyColor}"将起作用。

+0

与大多数情况一样,当你现在正确地做到这一点时,很容易。谢谢。 –

1

好吧,首先,你MyButton控制应该有一个DependencyProperty,比方说,MyColor - 你有吗?在您的模板中,您应该使用TemplateBinding绑定到该属性。在使用XAML来控制你使用它就像你正在做的:<local:MyButton Width="130" MyColor="Red"/>

看来你的模板缺少模板绑定:

<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> 

顺便说一句,MyColor应该是一个真正的Brush,不只是一个Color

+0

我有我的问题中所描述的依赖属性。当我尝试使用{TemplateBinding MyColor}绑定Fill属性时,我得到“在按钮类型中找不到属性MyColor” –

+0

@Magnus:比模板的目标类型是Button,而它必须是MyButton。 – Vlad

+0

我试过了,问题更新了。如果我将我的模板目标更改为MyButton,则会出现另一个错误:在'MyButton'类型中未找到属性'ContentTemplate'。 –