Search code examples
wpfattached-properties

Setting attached properties value with ChangePropertyAction


I'd like to know whether ChangePropertyAction in WPF supports modifying attached properties' value on target object? When trying to specify an attached property like Canvas.Top an exception is thrown (doesn't matter if its name is surrounded with parentheses or not). Thanks in advance for useful replies ;)


Solution

  • Unfortunately this doesn't seem to be possible. See this post http://forums.silverlight.net/t/201358.aspx

    If you put the relevant properties on your view model, e.g.

    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
    }
    

    and bind to those (Left, Top), then you can use CallMethodAction or ChangePropertyAction on the view model

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