2012-02-28 3 views
2

TextBox 및 PasswordBox가 포함 된 사용자 정의 사용자 정의 컨트롤을 만들었습니다. 그것은 완전히 바인딩 작업이지만 TextBox 또는 사용자 정의 컨트롤의 PasswordBox 내에서 값을 변경하면 내 소스 속성이 새로 고쳐지지 않습니다. 다음BindingMode.TwoWay가 UserControl (소스 속성 업데이트가 아님)에서 작동하지 않습니다.

RestrictedBox.xaml 내 사용자 정의 사용자 제어를위한

<UserControl.Resources> 
     <Converters:EnumToVisibilityConverter x:Key="enumToVisibilityConverter" /> 
     <Converters:EnumToVisibilityConverterReverse x:Key="enumToVisibilityConverterReverse" /> 
    </UserControl.Resources> 
    <Grid x:Name="LayoutRoot" Background="Transparent" > 
     <StackPanel> 
      <TextBox x:Name="txtTextBox" Width="50" Height="25" /> 
      <PasswordBox x:Name="txtPasswordBox" Width="50" Height="25" /> 
     </StackPanel> 
    </Grid> 

RestrictedBox.xaml.cs 코드입니다

public partial class RestrictedBox : UserControl 
    { 
     public RestrictedBox() 
     { 
      InitializeComponent(); 
      txtTextBox.SetBinding(TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay }); 
      txtTextBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type") 
       { 
        Source = this, 
        Converter = new EnumToVisibilityConverter() 
       }); 
      txtPasswordBox.SetBinding(PasswordBox.PasswordProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay }); 
      txtPasswordBox.SetBinding(TextBox.VisibilityProperty, new Binding("Type") 
      { 
       Source = this, 
       Converter = new EnumToVisibilityConverterReverse() 
      }); 
     } 
     public string Value 
     { 
      get { return (string)GetValue(ValueProperty); } 
      set { SetValue(ValueProperty, value); } 
     } 
     public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(RestrictedBox), new PropertyMetadata("", ValueChanged)); 
     private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 
     public Mode Type 
     { 
      get { return (Mode)GetValue(TypeProperty); } 
      set { SetValue(TypeProperty, value); } 
     } 
     public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(Mode), typeof(RestrictedBox), new PropertyMetadata(Mode.Text, TypeChanged)); 
     private static void TypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 
    } 

FOLLO 날개는 내 사용자 정의 사용자 정의 컨트롤 (RestrictedBox)을 사용하는 LoginView의 코드입니다.

LoginView.xaml

<control:RestrictedBox Type="Text" Value="{Binding Path=UserName}" /> 

LoginView.xaml.cs

[Export(typeof(LoginView))] 
    [PartCreationPolicy(CreationPolicy.NonShared)] 
    public partial class LoginView : UserControl, IPageTitle 
    { 
     #region Constuctors 
     public LoginView() 
     { 
      InitializeComponent(); 
     } 
     [Import] 
     public LoginViewModel ViewModel 
     { 
      get 
      { 
       return this.DataContext as LoginViewModel; 
      } 
      set 
      { 
       DataContext = value; 
      } 
     } 
     #endregion 
} 

LoginViewModel.cs

[Export] 
[PartCreationPolicy(CreationPolicy.NonShared)] 
public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime 
{ 
    private string _UserName = ""; 
    public string UserName 
    { 
     get { return _UserName; } 
     set 
     { 
      _UserName = value; 
      OnPropertyChanged("UserName"); 
     } 
    } 
    [ImportingConstructor] 
    public LoginViewModel(IEventAggregator eventAggregator, IRegionManager regionManager) 
    { 
    } 
} 

지난 1.5 일 이후 아무 문제없이 해결하려고하기 때문에이 문제를 해결하도록 도와주세요.

귀하의 의견과 제안은 높이 평가 될 것입니다.

참고 : - 나는 텍스트 상자에 사용자 이름의 값을 결합 할 수 있어요하지만 난 텍스트 상자를 업데이트하고 난 텍스트 상자에서 업데이트 된 값을 받고 없습니다 제출을 클릭합니다.

감사합니다,

Imdadhusen

+0

은 어디 값을 얻기 위해 노력하고있다 ??? LoginViewModel에서 사용자 이름과 암호를 가져 오시겠습니까 ?? – gaurawerma

+0

예. 내 LoginViewModel에서 그 값을 얻고 싶습니다. – imdadhusen

답변

2

당신은 LoginView.xaml 당신의 모드 = 양방향 누락 :

<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}" /> 
+0

많은 많은 감사, 마침내 나는 해결했다. +1 – imdadhusen

+0

내 투표를 듣고 좋은. :) – gaurawerma