2009-12-31 2 views
0

사각형 표시 및 바인딩 뷰 모델 클래스의 너비, 높이, 각도는 XAML코드 변환에서 WPF 바인딩이 실패합니다.

<Rectangle 
    RenderTransformOrigin="0.5,0.5" 
    Fill="Black" 
    Width="{Binding Path=Width, Mode=TwoWay}" 
    Height="{Binding Path=Height, Mode=TwoWay}"> 
    <Rectangle.RenderTransform> 
    <RotateTransform Angle="{Binding Path=Angle, Mode=TwoWay}" /> 
    </Rectangle.RenderTransform> 
</Rectangle> 

에서 예상대로 작동합니다. 그러나 코드 숨김에서 사각형을 만들 때 높이 및 너비에 바인딩 할 수 있지만 각도가 아닙니다.

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    Binding bindH = new Binding("Height"); 
    bindH.Mode = BindingMode.TwoWay; 

    Binding bindW = new Binding("Width"); 
    bindW.Mode = BindingMode.TwoWay; 

    // DOES NOT WORK 
    // AND I DID TRY MANY OTHER COMBINATIONS 
    Binding bindA = new Binding("Angle"); 
    bindA.Mode = BindingMode.TwoWay; 

    Rectangle r1 = new Rectangle(); 
    SolidColorBrush myBrush = new SolidColorBrush(Colors.Black); 
    r1.Fill = myBrush; 
    r1.RenderTransformOrigin = new Point(0.5,0.5); 
    r1.SetBinding(Rectangle.WidthProperty, bindW); 
    r1.SetBinding(Rectangle.HeightProperty, bindH); 

** // 작동하지 않습니다. **

 r1.SetBinding(RenderTransformProperty, bindA); 

    LayoutPanel.Children.Add(r1);      // my custom layout panel 
} 

모든 도움을 주셨습니다.

답변

1

ViewModel의 각도 속성은 RotateTransform으로 노출되어야합니다. 는 VM의 사소한 구현은 다음과 같습니다

public class ViewModel 
{ 
    public RotateTransform Angle { get; set; } 
    public int Height { get; set; } 
    public int Width { get; set; } 

    public ViewModel() 
    { 
     Angle = new RotateTransform(10); 
     Height = 50; 
     Width = 100; 
    } 
} 

바인딩은 당신이 Window_Loaded 이벤트 핸들러를 작성했습니다 그대로 작동합니다. 이는 작동하는 XAML 버전에서 Rectangle의 RenderTransform 태그 내에 선언적으로 RotateTransform 객체를 지정하기 때문에 의미가 있습니다.

희망이 :) 당신은 사각형에 각도를 설정하는

0

을 할 수 있지만 RotateTransform의 각도를 설정해야합니다.

이 시도

,

RotateTransform rot = new RotateTransform(); 
r1.RenderTransform = rot; 
rot.SetBinding(RotateTransform.AngleProperty, bindA); 
+0

RotateTransform에는 SetBinding이라는 메서드가 없습니다. – Zamboni

0

이 작동하지 않는 바인딩과 RotateTransform의 SetValue는 호출. 이유를 모르겠다. 하지만 BindingOperations를 살펴 보겠습니다.

var rotateTransform = new RotateTransform(); 
BindingOperations.SetBinding(rotateTransform, RotateTransform.AngleProperty, new Binding("RotationAngle") { Source = myViewModel }); 
myControl.RenderTransform = rotateTransform; 

여기서 "RotationAngle"은 ViewModel의 이중 특성입니다.

FYI에서 XAML의 바인딩은 결국 작동하는 것으로 보입니다. 몇 가지 바인딩 오류가 처음에는 다음 작동합니다 ... 그리고 네, 내 DataContext 장소에 잘 전에 RotateTransform 개체가 만들어집니다. Binding 오류를 피하기 위해 코드 숨김을 사용하고 Loaded 이벤트 동안 Binding을 설정했습니다.

관련 문제