2011-09-30 4 views
2

웹에서 답을 찾아 보았지만 제대로 작동하지 않는 것 같습니다.INotifyPropertyChanged, UI 업데이트 없음

public class UIValues : INotifyPropertyChanged 
    { 
     private double zoomValue = 1; 

     private static readonly UIValues instance = new UIValues(); 

     public event PropertyChangedEventHandler PropertyChanged; 

     internal static UIValues Instance { get { return instance; } } 

     internal double ZoomValue 
     { 
      get { return zoomValue; } 
      set 
      { 
       if (this.zoomValue == value) 
        return; 

       this.zoomValue = value; 
       this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue")); 
      } 
     } 

     protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
     { 
      if (this.PropertyChanged != null) 
       this.PropertyChanged(this, e); 
     } 
    } 

후 나는이있다 : 여기에 내가 무엇을 가지고 내가 코드 숨김에서 UIValues ​​클래스의 ZoomValue을 변경할 때마다

<UserControl> 
    <UserControl.DataContext> 
      <local:UIValues x:Name="uiValues"/> 
     </UserControl.DataContext> 

. 
. 
. 

       <Viewbox x:Name="vbViewBox" RenderTransformOrigin="0.5,0.5"> 
        <local:ImageControl x:Name="imgControl" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
        <Viewbox.RenderTransform> 
          <TransformGroup> 
            <CompositeTransform x:Name="trCompositeTransform" ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}" Rotation="0" SkewX="0" SkewY="0"/> 
          </TransformGroup> 
        </Viewbox.RenderTransform> 
       </Viewbox> 
</UserControl> 

그래서 기본적으로는 UI가 아닌를 업데이트되었습니다.

누구나 알 수 있습니까?

감사합니다.

답변

0

변경 액세스 한정자 모든 것이 잘 작동합니다 -을 : 대신 바인딩에 직접 Source을 지정할 수 있습니다.

+0

이것이 문제였습니다. 결정된! – Maximus

2

게시 된 코드를 보면 내가 줌 값을 변경하는 것과 같은 것으로 생각됩니다.

UIValues.Instance.ZoomValue = x; 

문제는이 XAML입니다 : -

<local:UIValues x:Name="uiValues"/> 

는 정적 Instance 속성에서 반환 동일한 인스턴스가 아닌 UIValues의 독립적 인 인스턴스를 구축합니다. 따라서 아무 것도 듣지 않는 객체의 값을 변경하게됩니다.

편집

또한 개의 zoomLevel는 바인딩 작업을 공개해야하고, internal이다.

해결책은 IApplicationService을 사용하고 App.xaml을 통해 작업을 수행하는 것입니다.

변경하면 클래스 :

public class UIValues : INotifyPropertyChanged, IApplicationService 
{ 
    private double zoomValue = 1; 

    public event PropertyChangedEventHandler PropertyChanged; 

    internal static UIValues Instance { get; private set; } 

    public double ZoomValue 
    { 
     get { return zoomValue; } 
     set 
     { 
      if (zoomValue == value) 
       return; 

      zoomValue = value; 
      this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue")); 
     } 
    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, e); 
    } 

    void IApplicationService.StartService(ApplicationServiceContext context) 
    { 
     Instance = this; 
     Application.Current.Resources.Add("UIValues", Instance); 
    } 

    void IApplicationService.StopService() { } 
} 

이 App.Xaml에 UIValues의 인스턴스를 추가 : - :

<UserControl ... DataContext="{StaticResource UIValues}"> 
-
<Application.ApplicationLifetimeObjects> 
    <local:UIValues /> 
</Application.ApplicationLifetimeObjects> 

그런 다음 사용자 정의 컨트롤이 같은 DataContext 설정

그건 비싸게해야 할 수도 있습니다 DataContext의 낭비라고 d를 실제 데이터로 변환합니다. 대중에게 ZoomValue 속성에 대한

<CompositeTransform ScaleX="{Binding ZoomValue, Source={StaticResource UIValues}}" ScaleY="{Binding ZoomValue, Source={StaticResource UIValues}}" Rotation="0" SkewX="0" SkewY="0"/>