在WPF中的资源字典中未更新RenderTransform

时间:2018-02-07 15:07:28

标签: wpf xaml

我想在资源字典中实现一个可以接收图像作为模板的旋钮。 当我在同一个库中实现它时,它运行良好:

knob.xaml:

<UserControl x:Class="SwitchesLibrary.Knob"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="96" Width="96" x:Name="This">
<Grid>
    <Image RenderTransformOrigin=".5,.5" Source="Images/Knobs/Knob.png" Height="96" Width="96" >
        <Image.RenderTransform>
            <RotateTransform Angle="{Binding Angle, ElementName=This}"/>
        </Image.RenderTransform>
    </Image>
</Grid>

knob.cs

public static readonly DependencyProperty AngleProperty =
       DependencyProperty.Register("Angle",
                                   typeof(double),
                                   typeof(ExampleKnobLocklessAnalogLarge),
                                    new FrameworkPropertyMetadata(0.0,
                                        FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                            null,
                                            coerceValueCallback));
public double Angle
{
    get { return (double)GetValue(AngleProperty); }
    set { SetValue(AngleProperty, value); }
}

但当我把它移到资源词典时:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:SwitchesLibrary">
<Style TargetType="local:Knob">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:Knob">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Image 
                            x:Name="image"
                            Source="{TemplateBinding KnobImage}"
                            RenderTransformOrigin=".5,.5" 
                            Stretch="Fill"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Image.RenderTransform>
                                <RotateTransform Angle="{TemplateBinding Angle}"/>
                            </Image.RenderTransform>
                        </Image>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

它似乎运行良好(角度值正确更新),但旋钮图像不旋转。为什么?

1 个答案:

答案 0 :(得分:3)

尝试将//base collection clients:{ //document client-1-store:{ users-allowed:[ "user-1-id", "user-2-id", "user-3-id", "user-4-id", ] } }替换为TemplateBinding绑定;

RelativeSource

<RotateTransform Angle="{Binding Angle, RelativeSource={RelativeSource AncestorType=local:Know}, Mode=OneWay}"/> 是绑定的优化版本,在所有情况下都不能很好地工作。