2011-03-03 31 views
2

我創建了一個自定義密碼框用戶控件,它可以顯示和隱藏密碼。它只是用一個綁定到相同密碼字符串屬性的文本框替換標準密碼框。這一切工作正常,但現在我的數據驗證錯誤不再顯示,雖然他們正在後臺正確生成。下面是從我的用戶控件的XAML:Silverlight IDataErrorInfo消息不顯示在自定義控件的文本框中

<UserControl x:Class="Controls.EAPPasswordBox" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400" x:Name="_root"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top"> 
     <PasswordBox x:Name="pwdBox" Password="{Binding Password, Mode=TwoWay,ValidatesOnDataErrors=True}" /> 
     <TextBox x:Name="txtBox" Text="{Binding Password, Mode=TwoWay,ValidatesOnDataErrors=True}" /> 
    </StackPanel> 
</Grid> 

下面是我如何使用它的一個觀點:

<local:EAPPasswordBox x:Name="pwdBox" 
       Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Password="{Binding password,Mode=TwoWay, ValidatesOnDataErrors=True}" ShowText="{Binding showPassword,Mode=TwoWay}"></local:EAPPasswordBox> 

父視圖的視圖模型,我們實現IDataErrorInfo的是這樣的:

public string this[string columnName] 
    { 
     get 
     { 
      string Result = ""; 
      switch(columnName.ToLower()) 
      { 
       case "password": 
        { 
         Result = Validatepassword(); 
         break; 
        } 
       case "password2": 
        { 
         Result = Validatepassword2(); 
         break; 
        } 
       default: 
        { 
         Result = this.ValidateStringValue(columnName); 

         break; 
        } 
      } 
      return Result; 
     } 
    } 

現在,當我在自定義密碼框中輸入文本時,驗證邏輯被稱爲很好但沒有顯示。我必須爲此調整我的用戶控制嗎?

編輯:這是我的passwordbox背後的代碼:

public partial class EAPPasswordBox : UserControl, INotifyPropertyChanged 
{ 
    public bool ShowText 
    { 

     get { return (bool)GetValue(ShowTextProperty); } 
     set { 

      SetValue(ShowTextProperty, value); 
       if (value == true) 
       { 
        this.pwdBox.Visibility = System.Windows.Visibility.Collapsed; 
        this.txtBox.Visibility = System.Windows.Visibility.Visible; 
       } 
       else 
       { 
        this.pwdBox.Visibility = System.Windows.Visibility.Visible; 
        this.txtBox.Visibility = System.Windows.Visibility.Collapsed; 
       } 
     } 

    } 

    public string Password 
    { 
     get { return (string)GetValue(PasswordProperty); } 
     set { SetValue(PasswordProperty, value); } 
    } 

    private Visibility _PwdBoxVisibility; 

    public Visibility PwdBoxVisibility 
    { 
     get { return _PwdBoxVisibility; } 
     set 
     { 
      _PwdBoxVisibility = value; NotifyPropertyChanged("PwdBoxVisibility"); 
     } 
    } 

    private Visibility _TxtBoxVisibility; 

    public Visibility TxtBoxVisibility 
    { 
     get { return _TxtBoxVisibility; } 
     set 
     { 
      _TxtBoxVisibility = value; NotifyPropertyChanged("TxtBoxVisibility"); 
     } 
    } 

    public static readonly DependencyProperty PasswordProperty = 
     DependencyProperty.Register("Password", typeof(string), typeof(EAPPasswordBox), null); 

    public static readonly DependencyProperty ShowTextProperty = 
     DependencyProperty.Register("ShowText", typeof(bool), typeof(EAPPasswordBox), new PropertyMetadata(OnShowTextPropertyChanged)); 

    public EAPPasswordBox() 
    { 
     InitializeComponent(); 
     this.pwdBox.SetBinding(PasswordBox.PasswordProperty, new System.Windows.Data.Binding() { Source = this, Path = new PropertyPath("Password"), Mode = BindingMode.TwoWay,ValidatesOnDataErrors=true }); 
     this.txtBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding() { Source = this, Path = new PropertyPath("Password"), Mode = BindingMode.TwoWay, ValidatesOnDataErrors=true }); 

     this.ShowText = false; 
    } 


    private static void OnShowTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     EAPPasswordBox passwordBox = d as EAPPasswordBox; 

     if (passwordBox != null) 
     { 
      passwordBox.ShowText=(bool)e.NewValue; 
     } 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 


} 

2日編輯:這也將有助於如果有人只是向我解釋的父窗口的XAML綁定用戶控件的屬性的基礎知識/控制。我不太明白爲什麼用戶控件不會獲取相應父視圖viewmodel屬性的屬性更改事件,因爲它通過xaml綁定到這些屬性。

+0

我解決了它,將加載事件中的控件中的text/pw框直接綁定到viewmodel的屬性。我從父視圖的xaml傳遞控件的普通公共屬性的值,以確定綁定到哪個密碼屬性。稍後將發佈完整答案。 – hoetz 2011-03-10 08:21:00

回答

1

這是我最後的解決方案。由於我意識到用戶控件的DataContext自動是父視圖的ViewModel,我完全拋棄了Password依賴屬性的綁定。我在控件中引入了一個新參數,該參數必須設置爲父視圖模型的密碼屬性。然後,我使用此字符串在控件的加載事件中手動綁定文本框和密碼框。這裏是我的代碼:

public partial class EAPPasswordBox : UserControl, INotifyPropertyChanged 
{ 
    public bool ShowText 
    { 

     get { return (bool)GetValue(ShowTextProperty); } 
     set { 

      SetValue(ShowTextProperty, value); 
       if (value == true) 
       { 
        this.pwdBox.Visibility = System.Windows.Visibility.Collapsed; 
        this.txtBox.Visibility = System.Windows.Visibility.Visible; 
       } 
       else 
       { 
        this.pwdBox.Visibility = System.Windows.Visibility.Visible; 
        this.txtBox.Visibility = System.Windows.Visibility.Collapsed; 
       } 
     } 

    } 

    public string PasswordPropertyName { get; set; } 



    private Visibility _PwdBoxVisibility; 

    public Visibility PwdBoxVisibility 
    { 
     get { return _PwdBoxVisibility; } 
     set 
     { 
      _PwdBoxVisibility = value; NotifyPropertyChanged("PwdBoxVisibility"); 
     } 
    } 

    private Visibility _TxtBoxVisibility; 

    public Visibility TxtBoxVisibility 
    { 
     get { return _TxtBoxVisibility; } 
     set 
     { 
      _TxtBoxVisibility = value; NotifyPropertyChanged("TxtBoxVisibility"); 
     } 
    } 


    public static readonly DependencyProperty ShowTextProperty = 
     DependencyProperty.Register("ShowText", typeof(bool), typeof(EAPPasswordBox), new PropertyMetadata(OnShowTextPropertyChanged)); 

    public EAPPasswordBox() 
    { 
     InitializeComponent(); 
     this.ShowText = false; 
    } 


    private static void OnShowTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     EAPPasswordBox passwordBox = d as EAPPasswordBox; 

     if (passwordBox != null) 
     { 
      passwordBox.ShowText=(bool)e.NewValue; 
     } 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

    private void _root_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.pwdBox.SetBinding(PasswordBox.PasswordProperty, new System.Windows.Data.Binding() { Source = this.DataContext, Path = new PropertyPath(PasswordPropertyName), Mode = BindingMode.TwoWay, ValidatesOnDataErrors = true }); 
     this.txtBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding() { Source = this.DataContext, Path = new PropertyPath(PasswordPropertyName), Mode = BindingMode.TwoWay, ValidatesOnDataErrors = true }); 

    } 


} 

這裏是控件的XAML。

<UserControl x:Class="GAB.EAP2011.Controls.EAPPasswordBox" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400" x:Name="_root" Loaded="_root_Loaded"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top"> 
     <PasswordBox x:Name="pwdBox" /> 
     <TextBox x:Name="txtBox" /> 
    </StackPanel> 
</Grid> 

下面是如何使用它:

<local:EAPPasswordBox x:Name="pwdBox" 
       Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" PasswordPropertyName="password" ShowText="{Binding showPassword,Mode=TwoWay}"></local:EAPPasswordBox> 

現在,你有一個很好的密碼知名度切換器控制:) 評論贊賞!

+0

感謝您的好問題和答案。我已經根據你的想法實現了同樣的想法,但仍然出現錯誤。你能寄給我完整的代碼樣本嗎? – imdadhusen 2012-02-29 13:11:04

+0

我的投票+1,我也標記爲最喜歡的。 – imdadhusen 2012-02-29 13:11:40

相關問題