Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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

 
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)
        {
        }
    }
 
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
 
[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)
    {
    }
}
 
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
Posted 28-Feb-12 3:44am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Finally i got solution
 
I am missing Mode=TwoWay in LoginView.xaml:
<control:RestrictedBox Type="Text" Value="{Binding Path=UserName,Mode=TwoWay}">
 
Thanks,
Imdadhusen
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Maciej Los 180
2 Richard MacCutchan 115
3 arvind mepani 104
4 Tino Fourie 92


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 29 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid