BindingMode.TwoWay不能与UserControl一起使用(不更新源属性)

时间:2012-02-28 14:39:55

标签: mvvm silverlight-4.0 user-controls prism-4

我创建了包含TextBox和PasswordBox的自定义用户控件。它完全绑定,但当我更改用户控件的TextBox或PasswordBox中的任何值时,我的源属性不会刷新。

以下是我的自定义用户控件的代码

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)
        {
        }
    }

以下是我使用自定义用户控件(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

1 个答案:

答案 0 :(得分:2)

您在LoginView.xaml中缺少Mode = TwoWay:

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