问题描述
我创建了包含TextBox和PasswordBox的自定义用户控件。我将TextBox绑定到UserName和PassowrdBox。
UserName在我的LoginViewModel类中被定义为[必需]属性。现在,我的光标离开TextBox而不输入任何值,然后UserName属性fire 属性更改通知(INotifyPropertyChanged)
但不会将我的文本框(位于用户控件内)标记为红色边框。
以下是我的用户控件的代码。 / p>
RestrictedBox.xaml
网格x:Name =LayoutRootBackground =TransparentMargin =0>
< TextBox x:Name =txtTextBoxHorizontalAlignment =StretchHeight =25/>
< PasswordBox x:Name =txtPasswordBoxHorizontalAlignment =StretchHeight =25/>
< / Grid>
RestrictedBox.xaml.cs
public partial class RestrictedBox:UserControl
{
#region属性
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 bool可更新
{
get {return(bool)GetValue(UpdateableProperty ); }
set {SetValue(UpdateableProperty,value); }
}
public static readonly DependencyProperty UpdateableProperty = DependencyProperty.Register(Updateable,typeof(bool),typeof(RestrictedBox),new PropertyMetadata(UpdateableChanged));
private static void UpdateableChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
}
public bool可重命名
{
get {return(bool)GetValue(RedactableProperty ); }
set {SetValue(RedactableProperty,value); }
}
public static readonly DependencyProperty RedactableProperty = DependencyProperty.Register(Redactable,typeof(bool),typeof(RestrictedBox),新的PropertyMetadata(RedactableChanged));
private static void RedactableChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region构造函数
public RestrictedBox()
{
InitializeComponent();
txtTextBox.SetBinding(TextBox.TextProperty,new Binding {Source = this,Path = new PropertyPath(Value),Mode = BindingMode.TwoWay});
txtTextBox.SetBinding(TextBox.VisibilityProperty,new Binding(Redactable){Source = this,Converter = new BoolToVisibilityConverterReverse()});
txtPasswordBox.SetBinding(PasswordBox.PasswordProperty,new Binding {Source = this,Path = new PropertyPath(Value),Mode = BindingMode.TwoWay});
txtPasswordBox.SetBinding(TextBox.VisibilityProperty,new Binding(Redactable){Source = this,Converter = new BoolToVisibilityConverter()});
}
#endregion
}
以下是代码我使用我的自定义用户控件
LoginView.xaml
code>< Control:RestrictedBox x:Name =UserNameVerticalAlignment =TopTabIndex =2Grid.Row =1Grid.Column =1HorizontalAlignment =StretchHeight =40 Value ={Binding Path = LoginModelValue.UserName,Mode = TwoWay,ValidatesOnNotifyDataErrors = True,ValidatesOnExceptions = True,
ValidatesOnDataErrors = True,NotifyOnValidationError = True}Validatevalue:UpdateSourceTriggerHelper.UpdateSourceTrigger =TrueRedactable =True Updateable =True/>
LoginView.xaml.cs
[Export(typeof(LoginView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class LoginView:UserControl,IPageTitle
{
#region Constuctors
public LoginView()
{
InitializeComponent();
}
[导入]
public LoginViewModel ViewModel
{
get {return this.DataContext as LoginViewModel;}
set {DataContext = value;
}
#endregion
}
LoginViewModel .cs
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public类LoginViewModel:INotifyPropertyChanged,IRegionMemberLifetime
{
private LoginModel _LoginModelValue;
public LoginModel LoginModelValue
{
get {return _LoginModelValue; }
set
{
_LoginModelValue = value;
OnPropertyChanged(LoginModelValue);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate {};
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
void LoginModelValue_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e)
{
if(LoginModelValue.IsValidObject())
{
LoginCommand。 RaiseCanExecuteChanged();
IsEnabled = LoginModelValue.IsValidObject();
setIncorrectLogin(!IsEnabled);
}
}
#endregion
}
任何人都有想法为什么我没有得到我的自定义用户控件中的TextBox包围的红色边框?
任何帮助,建议和谢谢,
Imdadhusen
现在我使用以下代码解决了问题。我已经替换了以下行
txtTextBox.SetBinding(TextBox.VisibilityProperty,new Binding(Redactable){Source = this,Converter = new BoolToVisibilityConverterReverse()});
与
this.MapBinding(RestrictedControl.ValueProperty,txtTextBox,TextBox.TextProperty);
并添加以下代码。就是这样。
命名空间QSys.Library.Helpers
{
public static class FrameworkElementExtension
{
public static void MapBinding(此FrameworkElement元素,DependencyProperty firstProperty,FrameworkElement targetElement,DependencyProperty secondProperty)
{
BindingExpression firstExpression = element.GetBindingExpression(firstProperty);
if(firstExpression!= null&& firstExpression.ParentBinding!= null)
{
targetElement.SetBinding(secondProperty,firstExpression.ParentBinding);
}
}
}
}
特别感谢大家如何寻找这个。而且我也非常感谢Rakesh Gunijan(http://www.codeproject.com/Articles/293302/Silverlight-user-control-validation)如何表现得非常清楚。
谢谢,
Imdadhusen
I have created custom User Control which contain TextBox and PasswordBox. I bind TextBox to UserName and PassowrdBox also.The UserName is defined in my LoginViewModel class with [Required] attribute. Now my cursor is leaving from TextBox without entering any value then UserName property fire property changeed notifcation (INotifyPropertyChanged),but dose not mark my Textbox (which is inside the User Control) with Red border.
Following is code of my User Control.
RestrictedBox.xaml
<Grid x:Name="LayoutRoot" Background="Transparent" Margin="0" >
<TextBox x:Name="txtTextBox" HorizontalAlignment="Stretch" Height="25" />
<PasswordBox x:Name="txtPasswordBox" HorizontalAlignment="Stretch" Height="25" />
</Grid>
RestrictedBox.xaml.cs
public partial class RestrictedBox : UserControl
{
#region Properties
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 bool Updateable
{
get { return (bool)GetValue(UpdateableProperty); }
set { SetValue(UpdateableProperty, value); }
}
public static readonly DependencyProperty UpdateableProperty = DependencyProperty.Register("Updateable", typeof(bool), typeof(RestrictedBox), new PropertyMetadata(UpdateableChanged));
private static void UpdateableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public bool Redactable
{
get { return (bool)GetValue(RedactableProperty); }
set { SetValue(RedactableProperty, value); }
}
public static readonly DependencyProperty RedactableProperty = DependencyProperty.Register("Redactable", typeof(bool), typeof(RestrictedBox), new PropertyMetadata(RedactableChanged));
private static void RedactableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
#endregion
#region Constructors
public RestrictedBox()
{
InitializeComponent();
txtTextBox.SetBinding(TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay});
txtTextBox.SetBinding(TextBox.VisibilityProperty, new Binding("Redactable") { Source = this, Converter = new BoolToVisibilityConverterReverse() });
txtPasswordBox.SetBinding(PasswordBox.PasswordProperty, new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay });
txtPasswordBox.SetBinding(TextBox.VisibilityProperty, new Binding("Redactable") { Source = this, Converter = new BoolToVisibilityConverter() });
}
#endregion
}
Following is the code where i used my Custom User Control
LoginView.xaml
<Control:RestrictedBox x:Name="UserName" VerticalAlignment="Top" TabIndex="2" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Height="40" Value="{Binding Path=LoginModelValue.UserName, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, ValidatesOnExceptions=True,
ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Validatevalue:UpdateSourceTriggerHelper.UpdateSourceTrigger="True" Redactable="True" Updateable="True" />
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 LoginModel _LoginModelValue;
public LoginModel LoginModelValue
{
get { return _LoginModelValue; }
set
{
_LoginModelValue = value;
OnPropertyChanged("LoginModelValue");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
void LoginModelValue_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (LoginModelValue.IsValidObject())
{
LoginCommand.RaiseCanExecuteChanged();
IsEnabled = LoginModelValue.IsValidObject();
SetIncorrectLogin(!IsEnabled);
}
}
#endregion
}
Can anybody has idea why i am not getting Red Border surrounded with my TextBox which is inside my Custom User Control?
Any help, suggestions and comments would be highly appreciated!
Thanks,
Imdadhusen
Now i resolved issue using following code. I have replaced following line
txtTextBox.SetBinding(TextBox.VisibilityProperty, new Binding("Redactable") { Source = this, Converter = new BoolToVisibilityConverterReverse() });
with
this.MapBinding(RestrictedControl.ValueProperty, txtTextBox, TextBox.TextProperty);
and added following code. that's it.
namespace QSys.Library.Helpers
{
public static class FrameworkElementExtension
{
public static void MapBinding(this FrameworkElement element, DependencyProperty firstProperty, FrameworkElement targetElement, DependencyProperty secondProperty)
{
BindingExpression firstExpression = element.GetBindingExpression(firstProperty);
if (firstExpression != null && firstExpression.ParentBinding != null)
{
targetElement.SetBinding(secondProperty, firstExpression.ParentBinding);
}
}
}
}
I specially thanks to everybody how was looking for this. and i am also very thanksful Rakesh Gunijan (http://www.codeproject.com/Articles/293302/Silverlight-user-control-validation) how expain in very much clear.
Thanks,
Imdadhusen
这篇关于Silverlight 4中的用户控件不会显示验证,但红色边框不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!