Wpf Silverlight-我的datacontext/绑定路径需要在这里是什么?

Wpf Silverlight-我的datacontext/绑定路径需要在这里是什么?,wpf,silverlight,data-binding,datacontext,Wpf,Silverlight,Data Binding,Datacontext,在过去一周左右的时间里,我一直在Silverlight的图像编辑器上工作。这是我第一次遇到它,我还没有完全了解数据绑定和datacontext或mvvm。我有一个旋转方法,我希望能够将角度值从MainPage.xaml上的文本框传递到该方法。我的初始值设置为90,我的函数在单击图像时将图像旋转90度。文本框在运行时是空的,并且显然没有更新我的旋转角度 MainPage.xaml <Grid DataContext="{Binding Path=Project}" Height="70" N

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

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

我做错了什么?问题是我的datacontext还是我的绑定路径?它们应该是什么?格式化有点不对劲抱歉

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"); 
 } 

您应该发布ViewModel的代码。对不起,在我发布之前意外发布。您有任何绑定错误吗?(它们在运行项目时显示在“输出”窗口中)不,输出窗口中似乎没有任何错误。我的数据上下文或绑定路径是否错误?发布设置页面数据上下文的代码可能会很有用。唯一不同的是,现在如果我在文本框中键入一个角度并按下旋转按钮(它仍然旋转90度),但文本框现在被清除。当我首先运行它时,初始值90也不会显示在我的文本框中。当然,它不会改变,您在这个方法中创建了一个新的RotateTransform对象,该对象的名称与您要读取的名称相同,而是使用全局RotateElement对象,如下所示:this.RotateElement=new RotateTransform();我更新了我的anwserAlso,当你的应用程序启动时,你的RotateElement为空,所以你在xaml中的值不会绑定,直到该对象被初始化…啊,我应该看到的,谢谢。我也不应该设置RotateElement.Angle=90,因为每次调用该方法时它都会将其设置为90。除了元素的宽度和高度值在旋转时没有变化的一些奇怪行为外,它现在工作正常。虽然渲染效果很好,但图像仍按应有的方式保存。再次感谢
 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"); 
 }