BindingMode.TwoWay不适用于UserControl(不更新源属性)
我已经创建了包含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)
{
}
}
弗洛wing是我使用自定义用户控件(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天以来没有任何运气。
您的意见和建议将不胜感激。
注意: -我能够将UserName的值绑定到TextBox,但我更新TextBox并单击提交我无法从TextBox获取更新的值。
感谢,
Imdadhusen
你缺少模式=双向你LoginView.xaml:
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}" />
很多很多谢谢,最后我解决了。我的投票+1 – imdadhusen 2012-02-29 08:03:40
很好听。 :) – gaurawerma 2012-02-29 08:27:24
你是哪里试图获得价值???你想在LoginViewModel中获得用户名和密码? – gaurawerma 2012-02-29 06:07:18
是的。我想在我的LoginViewModel上获取这些值。 – imdadhusen 2012-02-29 06:52:29