2015-01-25 5 views
0

바인딩을 통해 사각형의 회전을 제어하는 ​​방법이 있습니까? 나는이 방법을 시도했지만 어떤 생각이 들지 않습니까?AngleProperty에 이중 속성 바인딩

// class Unit 
    private double _rotation; 
    public double rotation 
    { 
     get 
     { 
      return _rotation; 
     } 
     set 
     { 
      _rotation = value; 
      OnPropertyChanged("rotation"); 
     } 
    } 
    public Binding rotationBinding { get; set; } 

    // Controller class generating UI 
    private Rectangle GenerateUnit(Unit u) 
    { 

     Rectangle c = new Rectangle() { Width = u.size, Height = u.size }; 

     c.Fill = new ImageBrush(new BitmapImage(new Uri(@"..\..\Images\tank\up.png", UriKind.Relative))); 
     c.SetBinding(Canvas.LeftProperty, u.xBinding); 
     c.SetBinding(Canvas.TopProperty, u.yBinding); 

     RotateTransform rt = new RotateTransform(); 
     BindingOperations.SetBinding(rt, RotateTransform.AngleProperty, u.rotationBinding); 
     c.LayoutTransform = rt; 

     return c; 
    } 

X 및 Y 바인딩이 제대로 작동하므로 올바르게 구현 된 것 같습니다.

각도 속성을 바인딩하는 방법을 찾고 있습니다. 회전 속성을 변경하면 UI에서 사각형이 회전합니다. 애니메이션이 필요 없으며, 각도를 즉시 바꿀 수 있습니다.

감사

+1

어떻게 'rotationBinding'을 만드나요? 그 코드도 게시해야합니다. – Clemens

+0

예. 잘못 쓰여졌습니다 ... 쓸모없는 쓰레드가 유감입니다. – Safiron

답변

1

문제는 rotationBinding 것 같다. 당신은 당신의 Unit 클래스에 바인딩을 만들어야합니다

rotationBinding = new Binding("rotation"); 
rotationBinding.Source = this;// or instance of o your Unit class if you create rotationBinding outside Unit class 

그것은 나를 위해 작동 ...

+0

고마워요. 제가 조금 과로했습니다. rotationBinding에 실수가있었습니다. – Safiron

0
당신이 XAML를 통해 모든 것을 할 수있을 때 코드에서 바인딩 만들기에 대한 조언을 것

:

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid Margin="10"> 
     <Slider x:Name="AngleProvider" 
       VerticalAlignment="Bottom" 
       Minimum="0" 
       Maximum="360" /> 
     <Rectangle Fill="Blue" 
        Margin="100" 
        RenderTransformOrigin="0.5,0.5"> 
      <Rectangle.RenderTransform> 
       <RotateTransform Angle="{Binding Value, ElementName=AngleProvider}" /> 
      </Rectangle.RenderTransform> 
     </Rectangle> 
    </Grid> 
</Window> 

이것은 창 가운데에 Rectangle 요소를 표시하고 하단에는 Slider 컨트롤을 표시합니다. 슬라이더를 드래그하여 사각형의 회전 각도를 변경하십시오.

바인딩 할 대상은 회전 각도입니다. 여기에서는 슬라이더를 사용하여 그 각도를 제공하지만 분명히 창이나 ViewModel 등의 코드 숨김의 속성에서 가져올 수 있습니다.