Click here to Skip to main content
15,938,448 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created sample User Control
RestrictedBox.xaml
XML
<UserControl.Resources>
       <Converters:EnumToVisibilityConverter x:Key="enumToVisConverter" />
       <Converters:EnumToVisibilityConverterReverse x:Key="enumToVisConverterReverse" />
   </UserControl.Resources>
   <Grid x:Name="LayoutRoot" Background="White" Width="Auto">
       <StackPanel Margin="0">
           <TextBox Text="{Binding Value}" Visibility="{Binding Type,Converter={StaticResource enumToVisConverter}}" BorderBrush="Green" />
           <PasswordBox Password="{Binding Value}" Visibility="{Binding Type,Converter={StaticResource enumToVisConverterReverse}}" BorderBrush="Red" />
       </StackPanel>
   </Grid>

RestrictedBox.xaml.cs
C#
public partial class RestrictedBox : UserControl
    {
        public RestrictedBox()
        {
            InitializeComponent();
            //If i bind static value and uncomment following line then it is working.
            //If i bind static value and comment following line then it is not working
            this.DataContext = this;
            //Dynamic binding does not work using this code.
        }

        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(TypeChanged));
        private static void TypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }

LoginViewModel.cs
C#
public class LoginViewModel : INotifyPropertyChanged, IRegionMemberLifetime
{
        private string _UserName = "Imdadhusen Sunasara";
        public string UserName
        {
            get { return _UserName; }
            set
            {
                _UserName = value;
                OnPropertyChanged("UserName");
            }
        }
}

LoginView.xaml (This following line does not work with binding)

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


This is working (with static binding)

HTML
<control:RestrictedBox Value="Imdadhusen" Type="Text" />


Thanks,
Imdadhusen
Posted

1 solution

That happens because you set the DataContext of the UserControl to itself. So, when the binding is applied, it searches the UserName property in the UserControl (and the property doesn't exist).


You can bind to a property of the UserControl by giving a name to the UserControl and, use the ElementName property of the Binding, like the following:


XML
<UserControl 
    ...
    x:Name="myControl"
    ...
    >
        ...
        <TextBox Text="{Binding Value, ElementName=myControl}" 
                    BorderBrush="Green" />
        ...
</UserControl>

Don't forget to remove the this.DataContext = this; line from the constructor of the UserControl. :)

 
Share this answer
 
Comments
Sunasara Imdadhusen 28-Feb-12 9:23am    
Good solution :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900