与“用户控件"的动态绑定不起作用,因为“静态"在Silverlight和MVVM中起作用 [英] Dynamic binding with User Control does not work as Static is working in Silverlight and MVVM

查看:90
本文介绍了与“用户控件"的动态绑定不起作用,因为“静态"在Silverlight和MVVM中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了示例用户控件
RestrictedBox.xaml

I have created sample User Control
RestrictedBox.xaml

<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


RestrictedBox.xaml.cs

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


LoginViewModel.cs

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


LoginView.xaml (此以下行不适用于绑定)


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

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



这是有效的(具有静态绑定)



This is working (with static binding)

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



谢谢,
Imdadhusen



Thanks,
Imdadhusen

推荐答案

发生这种情况是因为您将UserControlDataContext设置为自身.因此,当应用绑定时,它将在UserControl中搜索UserName属性(并且该属性不存在).

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).

您可以通过给UserControl命名来绑定到UserControl的属性,并使用BindingElementName属性,如下所示:

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:

<UserControl 

    ...

    x:Name="myControl"

    ...

    >
        ...
        <TextBox Text="{Binding Value, ElementName=myControl}" 

                    BorderBrush="Green" />
        ...
</UserControl>

别忘了从UserControl的构造函数中删除this.DataContext = this;行. :)

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


这篇关于与“用户控件"的动态绑定不起作用,因为“静态"在Silverlight和MVVM中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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