2012-02-28 2 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

関連する問題