generic.xaml 中的 TemplateBinding 和属性上的重绘已更改

发布于 2024-11-19 23:59:22 字数 1591 浏览 8 评论 0原文

我有一个自定义控件。在自定义控件中,有几个元素。这些元素之一应该具有特殊的高度值。

我所说的这个高度是以下代码中的 CanvasThickness:

private double canvasThickness;

public static readonly DependencyProperty CanvasThicknessProperty =
  DependencyProperty.Register("CanvasThickness",
  typeof(double),
  typeof(CustomControl1),
  new FrameworkPropertyMetadata(3d));

public double CanvasThickness
{
  get { return canvasThickness; }
  set { canvasThickness = value; }
}

在 generic.xaml 中,这个 CanvasThickness 用于 Canvas 的 Height-Property:

<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
  <Canvas Width="25" Height="{TemplateBinding local:CustomControl1.CanvasThickness}" Background="Green">
    // Templating Slider
  </Canvas>
</ControlTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Canvas Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" />
        </Canvas>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

现在这个 Slider 的高度为 3。 假设我像这样使用 CustomControl:

<ctrl:CustomControl1 CanvasThickness="12"/>

I会假设 Slider 的高度为 12。但它仍然是 3。CanvasThickness 的值为 12。

如何重新绘制 CustomControl属性改变了?我尝试了 FrameworkPropertyMetadataOptions,但它不影响 CustomControl。

提前致谢。

编辑:如果可能,解决方案也应该在 Silverlight 中运行。

I've a custom control. In the custom controls, there are several elements. One of those element should have a special height value.

This height I'm talking about is CanvasThickness in the following code:

private double canvasThickness;

public static readonly DependencyProperty CanvasThicknessProperty =
  DependencyProperty.Register("CanvasThickness",
  typeof(double),
  typeof(CustomControl1),
  new FrameworkPropertyMetadata(3d));

public double CanvasThickness
{
  get { return canvasThickness; }
  set { canvasThickness = value; }
}

In generic.xaml is this CanvasThickness used for the Height-Property of a Canvas:

<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
  <Canvas Width="25" Height="{TemplateBinding local:CustomControl1.CanvasThickness}" Background="Green">
    // Templating Slider
  </Canvas>
</ControlTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Canvas Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" />
        </Canvas>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Now this Slider has a height of 3. Let's say I use the CustomControl like this:

<ctrl:CustomControl1 CanvasThickness="12"/>

I would assume, that the Slider would have a Height of 12. But it's still 3. The value of CanvasThickness is 12.

How do I repaint the CustomControl on PropertyChanged? I tried FrameworkPropertyMetadataOptions, but it does not affect the CustomControl.

Thanks in advance.

EDIT: If possible, the solution should also run in Silverlight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陌上青苔 2024-11-26 23:59:22

这很简单:)

<Style x:Key="CanvasStyle" TargetType="{x:Type local:CustomCanvas}">
  <Setter Property="Height" Value="{Binding Path=CanvasThickness, RelativeSource={RelativeSource AncestorType={x:Type local:CustomControl1}}}" />
</Style>

<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
  <local:CustomCanvas Style="{StaticResource CanvasStyle}" Background="Green" />
</ControlTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Canvas Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" />
        </Canvas>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

It's simple :)

<Style x:Key="CanvasStyle" TargetType="{x:Type local:CustomCanvas}">
  <Setter Property="Height" Value="{Binding Path=CanvasThickness, RelativeSource={RelativeSource AncestorType={x:Type local:CustomControl1}}}" />
</Style>

<ControlTemplate x:Key="SliderTemplate" TargetType="{x:Type Slider}">
  <local:CustomCanvas Style="{StaticResource CanvasStyle}" Background="Green" />
</ControlTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Canvas Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" />
        </Canvas>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
撩发小公举 2024-11-26 23:59:22

Slider 模板中的 TemplateBinding 正在尝试解析模板化控件上名为 CanvasThickness 的属性,该控件是一个 Slider,不是您的自定义控件。

我一开始就不明白 Canvas 的意义。如果不确切知道您想要实现什么目标,就不可能说出来,但我怀疑您想要更接近此的东西:

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Border Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" Height="{TemplateBinding CanvasThickness}" />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

即使如此, Border 也具有硬编码的宽度和高度,这通常是一个坏主意。

The TemplateBinding in your Slider template is attempting to resolve a property called CanvasThickness on the templated control, which is a Slider, not your custom control.

I don't understand the point of the Canvas in the first place. It's impossible to say without knowing exactly what you're trying to achieve, but I suspect you want something closer to this:

<Style TargetType="{x:Type local:CustomControl1}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:CustomControl1}">
        <Border Width="50" Height="20" Background="GreenYellow">
          <Slider Template="{StaticResource SliderTemplate}" Height="{TemplateBinding CanvasThickness}" />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Even then, the Border has hard-coded widths and heights which is generally a bad idea.

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