3

我想知道 WPF 中的 ChangePropertyAction 是否支持修改目标对象的附加属性值?当试图指定像 Canvas.Top 这样的附加属性时,会引发异常(不管它的名称是否用括号括起来)。在此先感谢您的有用回复;)

4

1 回答 1

1

不幸的是,这似乎是不可能的。见这篇文章 http://forums.silverlight.net/t/201358.aspx

如果您将相关属性放在视图模型上,例如

public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        Left = 50;
        Top = 50;
    }

    public void AddOneHundred()
    {
        Left += 100;
    }

    private double _left;
    public double Left
    {
        get { return _left; }
        set
        {
            _left = value;

            RaisePropertyChanged("Left");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public double Top { get; set; }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

并绑定到那些(左,上),然后您可以在视图模型上使用 CallMethodAction 或 ChangePropertyAction

    <Canvas>
        <Canvas x:Name="theCanvas">
            <TextBlock Text="TextBlock" Canvas.Top="{Binding Top}" Canvas.Left="{Binding Left}"/>

            <Button Content="Button" Canvas.Left="48" Canvas.Top="96" Width="75">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <!--<ei:CallMethodAction TargetObject="{Binding}" MethodName="AddOneHundred"/>-->
                        <ei:ChangePropertyAction TargetObject="{Binding}" PropertyName="Left" Value="200"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </Canvas>
    </Canvas>   
于 2012-04-18T16:57:24.053 回答