简体   繁体   English

如何在自定义模板中使用自定义属性?

[英]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”) MyColorMyColor属性?

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.

相关问题 如何在自定义SSIS数据流组件中使用复选框列表来定制属性? - How can I use a Check Box List for custom property in a custom SSIS data flow component? 如何获得自定义属性的值? - How can i get the value of the custom property? 获取要在自定义属性中使用的属性值 - Get property value to use within custom attribute 使用数据注释时如何使用自定义编辑器模板? - How can I use a custom editor template when using data annotations? 如何在项目(而非源)文件中使用通过TemplateWizard收集的自定义模板参数? - How can I use custom template parameters gathered with TemplateWizard in the project (not source) file? 我可以将Dictionary类型用作自定义服务器控件的(persistet)属性吗? - Can I use the Dictionary type as a (persistet) property of a custom server control? 如何将 NUnit 的 EqualTo().Within() 约束与自定义数据类型一起使用? - How can I use NUnit's EqualTo().Within() constraint with a custom data type? EF Core 5 - 如何将 EF.Functions.Like 与映射到 JSON 字符串的自定义属性一起使用? - EF Core 5 - How can I use EF.Functions.Like with a custom property that maps to a JSON string? 如何在NHibernate中使用自定义对象 - How can I use custom objects with NHibernate 如何在自定义属性中找到修饰的方法? - How can I find the decorated method from within a custom attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM