将控制属性绑定到样式



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

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

我想要的是能够将旋转器上的文本设置为我喜欢的任何时间。目前,我将其用作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); }
        }

由于样式针对控件的类型,我无法模板绑定到我在繁忙的eCorator类型上创建的依赖项属性。如果我将样式的目标类型更改为"繁忙的代表",则说它找不到模板属性?

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

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

a Decorator没有任何模板,您不能将Control的CC_11 CC_11应用于Decorator

最新更新