2010-12-08 8 views
1

WPF에서 사용자 지정 컨트롤을 만들었으며 올바르게 바인딩하는 데 문제가 있습니다. 코드의 속성 값을 명시 적으로 설정하면 모든 것이 잘 작동합니다. 다음과 같이WPf 바인딩 문제

<TextBox Name="txtText" Grid.Row="0" Grid.Column="0" IsReadOnly="True" Text="{Binding Text, Mode=OneWay}" /> 

그리고 코드의 관련 속성 뒤에 다음과 같습니다 : 지금

public static readonly DependencyProperty TextConverterProperty = DependencyProperty.Register("TextConverter", typeof(IValueConverter), typeof(Selector)); 
public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector)); 

public string Text 
{ 
    get 
    { 
     string result = this.EntityId.ToString(); 

     if (this.TextConverter != null) 
     { 
      result = this.TextConverter.Convert(result, null, null, null) as string; 
     } 

     return result; 
    } 
} 

public long EntityId 
{ 
    get 
    { 
     return (long)this.GetValue(EntityIdProperty); 
    } 
    set 
    { 
     this.SetValue(EntityIdProperty, value); 

     this.OnPropertyChanged("Text"); 
     this.OnPropertyChanged("EntityId"); 
    } 
} 

public IValueConverter TextConverter 
{ 
    get 
    { 
     return this.GetValue(TextConverterProperty) as IValueConverter; 
    } 
    set 
    { 
     this.SetValue(TextConverterProperty, value); 
    } 
} 

그리고 내 페이지의 XAML 구현 : 여기에

내 컨트롤 내의 TextBox에 대한 XAML의

<controls:Selector x:Name="txtReferringCase" EntityId="{Binding ReferringDACaseId}" TextConverter="{StaticResource daCaseNumberConverter}" Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="3" ButtonClicked="txtReferringCase_ButtonClicked" /> 

이제 이상한 부분이 있습니다. 나는 페이지의 DataContext를 설정할 수 있으며, 아무 일도 발생하지 않습니다,하지만 난 주석 행의 주석을 해제 할 때 텍스트가 문제없이 내 사용자 컨트롤에 표시 :

_caseScreen = new DACaseScreen(itemId); 
this.DataContext = _caseScreen; 
//this.txtReferringCase.EntityId = _caseScreen.ReferringDACaseId; 

편집 : 내가 얘기를 깜빡 했네요 또 한가지 .. . 중단 점을 누르고 컨트롤의 EntityId 및 Text 속성을 확인하면 두 값 모두 예상 한 값이 표시됩니다. UI가 업데이트되지 않는 것 같습니다.

+0

'DataContext'를 변경하면 어떻게 될 것으로 예상됩니까? – Jon

답변

1

게터 및 DependencyProperties에 대한 세터를 편의상. 추가 동작을 원할 경우 DependencyProperty을 원하는대로 변경 핸들러에 등록하십시오.

public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector),new UIPropertyMetadata(EntityIdChanged)); 

public long EntityId 
{ 
    get 
    { 
     return (long)this.GetValue(EntityIdProperty); 
    } 
    set 
    { 
     this.SetValue(EntityIdProperty, value); 
    } 
} 

private static void EntityIdChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    var control = (Selector)sender; 
    control.OnPropertyChanged("Text"); 
} 

종속성 속성에는 PropertyChanged을 호출 할 필요가 없습니다. 또한 UserControl 컨트롤 자체에 소스를 설정하는 txtText 바인딩을 변경해야하지만 코드의 속성을 설정하면 문제가되지 않습니다. Selector라는 컨트롤이 내장되어 있지만 중요하지는 않은 컨트롤의 이름을 바꿀 수 있습니다.

+0

Perfect. 고맙습니다! –

0

OnPropertyChanged 전화를 걸기 전까지는 UI가 업데이트되지 않습니다. DataContext으로 설정하면 작동하지 않습니다.

public static readonly DependencyProperty TextConverterProperty = DependencyProperty.Register("TextConverter", typeof(IValueConverter), typeof(Selector)); 
public static readonly DependencyProperty EntityIdProperty = DependencyProperty.Register("EntityId", typeof(long), typeof(Selector)); 

하지만 당신은 텍스트 의존성 만 TextConverter 및 ENTITYID를 등록 해달라고 : 또한

당신이 할. 의미, 당신의 OnPropertyChanged (?)가 작동하지 않습니다 (또는 그냥 :) 여기에 선을 표시하는 것을 잊지 않았다) XAML들이 전용을 사용하지 않기 때문에 단지 GetValueSetValue를 호출해야

+0

**에 ** ** 바인딩하지 않으면 텍스트가 종속되지 않습니다. 종속성 속성이 아닐지라도 "Text"에서 OnPropertyChanged를 실행할 수 있습니다. – Vitalik