0

过去一周左右,我一直在使用 Silverlight 中的图像编辑器。这是我第一次遇到它,我还没有完全理解数据绑定和数据上下文或 mvvm。我有一个旋转方法,我希望能够将 MainPage.xaml 上的文本框中的角度值传递给该方法。我的初始值设置为 90,当我单击它时,我的函数将图像旋转 90 度。文本框在运行时为空,并且显然没有更新我的旋转角度。

主页.xaml

<Grid DataContext="{Binding Path=Project}" Height="70" Name="grid1" Width="200">
   <Grid.RowDefinitions>
      <RowDefinition Height="35" />
      <RowDefinition Height="35" />
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="84*" />
      <ColumnDefinition Width="57*" />
      <ColumnDefinition Width="59*" /> 
   </Grid.ColumnDefinitions>

<Button Command="{Binding Path=RotateCWElementCommand}"
      Height="30" Name="btnRotCW" Width="30" Grid.Column="2" Margin="15,0,14,5" Grid.Row="1">
<Image Source="../Assets/Images/Icons/object-rotate-right.png" Grid.Column="1"
      Grid.Row="4" Height="20" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="20" />
</Button>

<TextBlock FontWeight="Bold" HorizontalAlignment="Left" Margin="10,10,0,8" Text="Rotate" VerticalAlignment="Center" />
<TextBlock FontWeight="Bold" HorizontalAlignment="Left" Margin="34,9,0,10" Text="Angle:" VerticalAlignment="Center" Grid.Row="1" />
<TextBox Text="{Binding Path=RotateElement.Angle, Mode=TwoWay}" Height="24" Name="textBox1" Width="52" Grid.Column="1" Margin="0,6,5,5" Grid.Row="1" />
</Grid>

(相关代码来自)Project.cs-

namespace ImageEditor.Client.BLL
{
public class Project : INotifyPropertyChanged
{

            #region Properties
            private RotateTransform rotateElement;
      public RotateTransform RotateElement
      {
        get { return rotateElement; }
        set
        {
            rotateElement = value;
            NotifyPropertyChanged("RotateElement");

        }
    }

        #endregion
    #region Methods


    private void RotateCWElement(object param)
    {
        FrameworkElement element = this.SelectedElement;
        RotateTransform RotateElement = new RotateTransform();

        RotateElement.Angle = 90;
        RotateElement.CenterX = element.ActualWidth * 0.5;
        RotateElement.CenterY = element.ActualHeight * 0.5;
        element.RenderTransform = RotateElement;


    }

我在这里做错了什么?问题是我的数据上下文还是我的绑定路径?它们应该是什么?格式化有点不对劲

4

1 回答 1

2

UI 不知道对象中的属性已更改,因为您仅在对象更改时通知,而不是其中的属性:

 private void RotateCWElement(object param) 
 { 
        FrameworkElement element = this.SelectedElement; 
        if (this.RotateElement == null) this.RotateElement = new RotateTransform(); 

        RotateElement.Angle = 90; 
        RotateElement.CenterX = element.ActualWidth * 0.5; 
        RotateElement.CenterY = element.ActualHeight * 0.5; 
        element.RenderTransform = RotateElement; 

        //tell the UI that this property has changed
        NotifyPropertyChanged("RotateElement"); 
 } 
于 2012-07-19T10:29:16.627 回答