2012-07-03 2 views
0

INotifyPropertyChanged를 구현하는 VM의 Point 속성에 바인딩 된 Usercontrol (InfoControl)에 2 개의 TextBox가 있습니다.wpf usercontrol 바인딩이 TextBox가 수행하는 동안 즉시 업데이트되지 않음

다른 가능한 UserControl (DesignerControl)에는 드래그 가능한 사각형이 있습니다. Rectangle의 Canvas.Left와 Canvas.Bottom은 동일한 VM의 ConvPoint 속성에 바인딩됩니다. ConvPoint 속성 (0 => ActualWidth)은 직사각형을 드래그하면 ConvPoint VM 속성과 텍스트 상자의 값이 즉시 업데이트되지만 포인트 속성 (0 => 1)

의 변환 된 버전입니다. 내 텍스트 상자를 새 값으로 업데이트하면 VM Point 속성이 즉시 업데이트되지만 직사각형이 즉시 드래그 될 때만 배치됩니다.

코드의 비트 설명하기 : 이

첫째, 내 뷰 모델의 위치 속성

public class MyVM : ViewModelBase 
{ 
    private DependencyPoint position; 
    public DependencyPoint Position 
    { 
     get { return this.position; } 
     set 
     { 
      this.position = value; 
      RaisePropertyChanged("Position"); 
     } 
    } 

    public DependencyPoint ConvPosition 
    { 
     get { return new Point(this.Position.X * MainVM.ActualWidth, this.Position.Y * MainVM.AcutalHeight);} 
     set 
     { 
      Point p = new Point(value.X/MainVM.ActualWidth,value.Y/MainVM.ActualHeight); 
      this.position = p; 
      RaisePropertyChanged("ConvPosition"); 
     } 
    } 
} 


편집 : 나는 이것에 대한 클래스 DependencyPoint을 사용하고 상의 통지를 가지고 X 및 Y 속성 :

public class DependencyPoint : DependencyObject 
{ 

    public enum PointOrder 
    { 
     isStartPoint, 
     isEndPoint, 
     isPoint1, 
     isPoint2 
    } 

    public DependencyPoint() 
    { 
    } 

    public DependencyPoint(Double x, Double y, PointOrder po) 
    { 
     this.X = x; 
     this.Y = y; 
     this.Order = po; 
    } 

    public DependencyPoint(DependencyPoint point) 
    { 
     this.X = point.X; 
     this.Y = point.Y; 
     this.Order = point.Order; 
    } 


    public DependencyPoint(Point point, PointOrder po) 
    { 
     this.X = point.X; 
     this.Y = point.Y; 
     this.Order = po; 
    } 

    public Point ToPoint() 
    { 
     return new Point(this.X, this.Y); 
    } 



    public PointOrder Order 
    { 
     get { return (PointOrder)GetValue(OrderProperty); } 
     set { SetValue(OrderProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Order. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty OrderProperty = 
     DependencyProperty.Register("Order", typeof(PointOrder), typeof(DependencyPoint), new UIPropertyMetadata(null)); 



    public Double X 
    { 
     get { return (Double)GetValue(XProperty); } 
     set { SetValue(XProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for X. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty XProperty = 
     DependencyProperty.Register("X", typeof(Double), typeof(DependencyPoint), new UIPropertyMetadata((double)0.0)); 



    public Double Y 
    { 
     get { return (Double)GetValue(YProperty); } 
     set { SetValue(YProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Y. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty YProperty = 
     DependencyProperty.Register("Y", typeof(Double), typeof(DependencyPoint), new UIPropertyMetadata((double)0.0)); 
} 


012 다음 내 InfoControl에서 3,516,:

<Grid Grid.Row="0" DataContext="{Binding Position}"> 
       <Grid.RowDefinitions> 
        <RowDefinition/> 
        <RowDefinition/> 
       </Grid.RowDefinitions> 
       <DockPanel Margin="3,3,0,1" Grid.Row="0" LastChildFill="True"> 
        <TextBlock Foreground="White" Padding="0,3,0,0" Margin="2,0,0,0" DockPanel.Dock="Left" Text="StartPoint.X :"/> 
        <TextBox FontWeight="DemiBold" Foreground="Black" Background="#efefef" Width="Auto" Margin="7,0,0,0" Text="{Binding X, Converter={StaticResource StDConverter}, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"/> 
       </DockPanel> 
       <DockPanel Margin="3,3,0,1" Grid.Row="1" LastChildFill="True"> 
        <TextBlock Foreground="White" Padding="0,3,0,0" Margin="2,0,0,0" DockPanel.Dock="Left" Text="StartPoint.Y :"/> 
        <TextBox FontWeight="DemiBold" Foreground="Black" Background="#efefef" Width="Auto" Margin="7,0,0,0" Text="{Binding Y, Converter={StaticResource StDConverter}, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"/> 
       </DockPanel> 
      </Grid> 

그리고 DesignerControl의 :

<UserControl> 
    <Canvas> 
     <Rectangle x:Name="Point" Width="10" Height="10" Canvas.Bottom="{Binding ConvPosition.Y, Mode=TwoWay}" Canvas.Left="{Binding ConvPosition.X, Mode=TwoWay}" /> 
    </Canvas> 
</UserControl> 

나는 actualWidth에 액세스 할 수 있으며 내 사각형이 아니라 캔버스에 배치됩니다.

VM의 일부 속성이나 조금 더러울 수도 있지만 제대로 변환하고 변환을 관리하는 다른 방법을 알지 못합니다.

어떤 아이디어가 있습니까?

답변

0

ConvPosition이 Position에 의존하기 때문에 Position setter에서 ConvPosition에 대한 NotificationChanged를 발생시켜야합니다. 이

public Point ConvPosition 
{ 
    get { return new Point(this.Position.X * MainVM.ActualWidth, this.Position.Y * MainVM.AcutalHeight); } 
    set 
    { 
     this.Position = new Point(value.X/MainVM.ActualWidth, value.Y/MainVM.ActualHeight); 
    } 
} 

편집

변경할 수 있습니다

public Point Position 
{ 
    get { return this.position; } 
    set 
    { 
     this.position = value; 
     RaisePropertyChanged("Position"); 
     RaisePropertyChanged("ConvPosition"); 
    } 
} 

ConvPosition 나는 MyVM에 두 개의 추가 속성이 simpliest 솔루션이 될 것이라고 생각합니다. 그리드

public double X 
{ 
    get { return Position.X; } 
    set 
    { 
     Position = new Point(value, Position.Y); 
     RaisePropertyChanged("X"); 
    } 
} 

public double Y 
{ 
    get { return Position.Y; } 
    set 
    { 
     Position = new Point(Position.X, value); 
     RaisePropertyChanged("Y"); 
    } 
} 

만 변화 :이 답변 aarh

<Grid DataContext="{Binding}"> 
+0

완벽한 듯하지만 불행히도, 그것은 작동하지 않습니다. 나는 지금 잃어버린 ... – Julien

+0

나는 마지막 문제가 있다고 생각한다 : [Point] (http://msdn.microsoft.com/en-us/library/bk9hwzbw) 구조입니다. INotifyPropertyChanged를 구현하지 않습니다.'MyVM' 클래스에서 새로운 Point Position을 변경할 때마다 생성하는 두 개의 추가 속성 인'X'와'Y'를 만들거나 만들 수있는 (자신의) 클래스를 사용하십시오. 텍스트 상자를 이들에 바인딩하십시오. – LPL

+0

INotifyPRopertyChanged를 구현 한 클래스를 사용해 보았습니다. DependencyPoint 클래스입니다. 왜냐하면 실제로 내 포인트 값을 전파 할 수 없었기 때문입니다. 그것이 내가 여기에 붙어있는 이유입니다. (죄송합니다. 내가 붙어있어서 잠시 동안 언급 한 것을 잊었습니다.) – Julien

관련 문제