2016-08-11 3 views
0

내가 이런 목록보기가 변경되었습니다. 스레드는 항목을 구문 분석하고 완료되면 필드를 업데이트합니다. 필드가 업데이트되면 OnPropertyChanged 메서드를 호출합니다. 내 UserControl local:StatusElement을 사용하는 것을 제외하고는 모든 필드에서 정상적으로 작동합니다. NAME과 같은 상태를 표시하려고 시도했지만 정확하게 새로 고칩니다. 그러나 local:StatusElement은 새로 고침이 없습니다. StatusElement.State에 대한 get/set에 대한 중단 점에 도달하지 않습니다.새로 고침 ListView에 항목이

내 UserControl을 :

<UserControl ... 
      x:Name="mainControl"> 
    <Grid Name="LabelGrid"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Image Grid.Column="0" Name="MyImage" 
       Source="{Binding Source, Source={StaticResource MyImage}}" 
       Width="{Binding Height, ElementName=mainControl}" 
       Height="{Binding Height, ElementName=mainControl}"/> 
     <Label Grid.Column="1" Name="statusLabel"/> 
    </Grid> 
</UserControl> 

과 :

public partial class StatusElement : UserControl 
{ 

    // Dependency property backing variables 
    public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", 
       typeof(String), typeof(StatusElement), new UIPropertyMetadata(null)); 

    private string _state = ""; 
    public String State 
    { 
     get 
     { 
      return _state; 
     } 
     set 
     { 
      _state = value; 
      RefreshState(); 
     } 
    } 

    private void RefreshState() 
    { 
     switch (State) 
     { 
      case "": 
       MyImage.Visibility = Visibility.Hidden; 
       break; 
      default: 
       MyImage.Visibility = Visibility.Visible; 
       break; 
     } 
     statusLabel.Content = State; 
    } 

    public StatusElement() 
    { 
     InitializeComponent(); 
     RefreshState(); 
    } 
} 

내 statusLabel의 내용이 새로 고쳐지지 않는 이유는 무엇입니까?

+0

단, 'UpdateSourceTrigger = PropertyChanged'는 단방향 바인딩에서 효과가 없습니다 (대상에서 소스로의 데이터 흐름이 없음). 그 외에도'Mode = OneWay'를 명시 적으로 설정하는 것은 보통 중복되는데, 이는 대부분의 의존성 속성에 대한 기본값이기 때문입니다. – Clemens

답변

2

귀하의 정의는 잘못된 것입니다.

아래 보이는 것처럼 CLR 속성 래퍼는 속성을 소유하는 DependencyObject의 GetValueSetValue 메서드를 호출해야합니다.

public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", 
    typeof(string), 
    typeof(StatusElement), 
    new PropertyMetadata(null, (o, e) => ((StatusElement)o).RefreshState())); 

public string State 
{ 
    get { return (string)GetValue(StateProperty); } 
    set { SetValue(StateProperty, value); } 
} 

PropertyMetadata 생성자에 대한 두 번째 인수에 유의하십시오. 람다 식으로 구현 된 정적 인 PropertyChangedCallback입니다.

+0

Perfect!를 실행하지 않습니다! 나는이 방법을 몰랐다. 고마워. –

1

클래스가 INotifyPropertyChanged 이벤트를 구현하지 않습니다. 업데이트가 구현되도록 구현하십시오.

속성 값이 변경되었음을 클라이언트에 알립니다. State 종속성 속성의

public partial class StatusElement : UserControl,INotifyPropertyChanged 
{ 
.... 

public event PropertyChangedEventHandler PropertyChanged; 

private void RefreshState([CallerMemberName]string prop = "") 
{ 
    switch (State) 
    { 
     case "": 
      MyImage.Visibility = Visibility.Hidden; 
      break; 
     default: 
      MyImage.Visibility = Visibility.Visible; 
      break; 
    } 
    statusLabel.Content = State; 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(prop)); 

    } 
} 
} 
+0

속성은 일반적으로 이미 기본 제공 변경 알림 메커니즘이있는 종속성 속성으로 정의되므로 일반적으로 UserControl에서 INotifyPropertyChanged를 구현할 필요가 없습니다. – Clemens

+0

내가 볼 수있는 튜토리얼이 있습니까? – Sadique

+0

Google "wpf custom dependency properties". – Clemens

관련 문제