将控件属性绑定到样式

时间:2018-03-28 14:23:54

标签: wpf xaml resourcedictionary

我有一个繁忙的装饰器,如下所示

<controls:BusyDecorator x:Name="Busy" BusyStyle="{StaticResource busyStyle}" FadeTime="00:00:00.2" IsBusyIndicatorShowing="{Binding IsBusy}" Tag="Description">

风格

<Style x:Key="busyStyle" TargetType="{x:Type Control}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Control}">
                <ControlTemplate.Resources>
                    <Style TargetType="{x:Type Rectangle}">
                        <Setter Property="Width" Value="8" />
                        <Setter Property="Height" Value="16" />
                        <Setter Property="Stroke" Value="Black" />
                        <Setter Property="StrokeThickness" Value="1" />
                        <Setter Property="RadiusX" Value="2" />
                        <Setter Property="RadiusY" Value="2" />
                        <Setter Property="RenderTransformOrigin" Value=".5,.5" />
                    </Style>
                </ControlTemplate.Resources>
                <Canvas Width="64" Height="64">
                    <Rectangle Canvas.Left="24">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="top" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="10" Canvas.Top="6">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="-45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="topLeft" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Top="24" Width="16" Height="8">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="left" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="10" Canvas.Top="34">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottomLeft" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="24" Canvas.Top="40">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottom" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="38" Canvas.Top="34">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="-45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottomRight" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Top="24" Canvas.Left="38" Width="16" Height="8">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="right" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="38" Canvas.Top="6">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="topRight" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock Canvas.Left="1" Canvas.Top="64" FontSize="11" Foreground="White" Text="{TemplateBinding Tag}" />
                </Canvas>

我想要的是能够在运行时将旋转器上的文本设置为我喜欢的任何内容。现在我很难在Tag属性上将其编码为POC,但我没有在微调器中显示任何文本。如果我改变了

{TemplateBinding Tag}

在样式的底部,以简单的硬编码文本值显示文本。

如何从父控件传播文本字符串以在样式呈现上显示?

更新

我有一个代表这个控件的类,如下所示

 [StyleTypedProperty(Property = "BusyStyle", StyleTargetType = typeof (Control))]
    public class BusyDecorator : Decorator
{
#region IsBusyIndicatorShowing Property

        /// <summary>
        /// Identifies the IsBusyIndicatorShowing dependency property.
        /// </summary>
        public static readonly DependencyProperty IsBusyIndicatorShowingProperty = DependencyProperty.Register(
            "IsBusyIndicatorShowing",
            typeof (bool),
            typeof (BusyDecorator),
            new FrameworkPropertyMetadata(false,
                                          FrameworkPropertyMetadataOptions.AffectsMeasure,
                                          OnIsShowingChanged));

        /// <summary>
        /// Gets or sets if the BusyIndicator is being shown.
        /// </summary>
        public bool IsBusyIndicatorShowing
        {
            get { return (bool) GetValue(IsBusyIndicatorShowingProperty); }
            set { SetValue(IsBusyIndicatorShowingProperty, value); }
        }

由于样式的目标是Control的类型,我无法将模板绑定到我在BusyDecorator类型上创建的依赖项属性。如果我将样式的TargetType更改为“BusyDecorator”,它说它找不到Template属性?

2 个答案:

答案 0 :(得分:1)

我相信,通过指定{TemplateBinding Tag} Textblock指的是自己的标记,您应该考虑在该控件中使用BusyContent作为DependencyProperty创建自定义控件。 然后,您可以指定{TemplateBinding BusyContent}

答案 1 :(得分:0)

BusyDecorator应该是Control

public class BusyDecorator : Control
...

...使用Template

<Style x:Key="busyStyle" TargetType="{x:Type local:BusyDecorator}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Control}">
                <ControlTemplate.Resources>
                    <Style TargetType="{x:Type Rectangle}">
                        <Setter Property="Width" Value="8" />
                        <Setter Property="Height" Value="16" />
                        <Setter Property="Stroke" Value="Black" />
                        <Setter Property="StrokeThickness" Value="1" />
                        <Setter Property="RadiusX" Value="2" />
                        <Setter Property="RadiusY" Value="2" />
                        <Setter Property="RenderTransformOrigin" Value=".5,.5" />
                    </Style>
                </ControlTemplate.Resources>
                <Canvas Width="64" Height="64">
                    <Rectangle Canvas.Left="24">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="top" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="10" Canvas.Top="6">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="-45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="topLeft" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Top="24" Width="16" Height="8">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="left" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="10" Canvas.Top="34">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottomLeft" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="24" Canvas.Top="40">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottom" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="38" Canvas.Top="34">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="-45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="bottomRight" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Top="24" Canvas.Left="38" Width="16" Height="8">
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="right" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle Canvas.Left="38" Canvas.Top="6">
                        <Rectangle.RenderTransform>
                            <RotateTransform Angle="45" />
                        </Rectangle.RenderTransform>
                        <Rectangle.Fill>
                            <SolidColorBrush x:Name="topRight" Color="#DFDFDF" />
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock Canvas.Left="1" Canvas.Top="64" FontSize="11" Foreground="Blue" Text="{TemplateBinding Tag}" />
                </Canvas>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后这个有效:

<local:BusyDecorator x:Name="Busy" Style="{StaticResource busyStyle}" Tag="Description" />

Decorator没有任何模板,您无法将Style TargetType Control的{​​{1}}应用于Decorator或者。