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

发布于 2024-12-10 12:16:46 字数 2113 浏览 0 评论 0原文

假设想要创建一个左侧有一个小椭圆的自定义按钮。 我希望这个椭圆的颜色是可数据绑定的。

因此,我启动 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 theBrush`:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-12-17 12:16:46

这里的问题是 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 your MyColor dependency property.

Open Themes/Generic.xaml and find the default style that has been placed there for this new MyButton control. Replace the Template in that style with your template. Now the Fill="{TemplateBinding MyColor}" in your template will work.

浊酒尽余欢 2024-12-17 12:16:46

好的,首先,您的 MyButton 控件应该有一个 DependencyProperty,例如 MyColor ——您有吗?在您的模板中,您应该使用 TemplateBinding 绑定到该属性。在使用控件的 XAML 中,您可以按照如下方式使用它:

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

<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

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 using TemplateBinding. 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文