BindingMode.TwoWay不适用于UserControl(不更新源属性) [英] BindingMode.TwoWay does not work with UserControl (not update source property)

查看:314
本文介绍了BindingMode.TwoWay不适用于UserControl(不更新源属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我创建了包含文本框和密码框的自定义用户控件.它完全可以绑定,但是当我更改用户控件的TextBox或PasswordBox内的任何值时,我的source属性就不会刷新.

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

RestrictedBox.xaml

Hi All,
I have created Custom User Control which contain TextBox and PasswordBox. it is binding completely work but when i changed any value inside TextBox or PasswordBox of user control then my source property does not getting refreshed.

Following are the code for my Custom User Control

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




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



Following are the code for LoginView where i have used my custom User Control (RestrictedBox).

LoginView.xaml

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



LoginView.xaml.cs



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



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



Please help me to resolved this because i am trying to resolve since last 1.5 days without any luck.

Your comments and suggestions would be highly appreciated.


Note:-
I am able to bind value of the UserName to TextBox but i update TextBox and click on submit i couldn''t getting updated value from TextBox.

Thanks,

Imdadhusen

推荐答案

最后我得到了解决方案

我在LoginView.xaml中缺少Mode = TwoWay:
Finally i got solution

I am missing Mode=TwoWay in LoginView.xaml:
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}">



谢谢,
Imdadhusen



Thanks,
Imdadhusen


这篇关于BindingMode.TwoWay不适用于UserControl(不更新源属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆