使用用户控件的动态绑定不起作用,因为Static在Silverlight和MVVM中工作

时间:2012-02-27 13:54:46

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

我创建了示例用户控件

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

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

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

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

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

这是有效的(带有静态绑定的

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

谢谢, Imdadhusen

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

实际上它应该有效。你能否验证下面控件的父容器的DataContext没有引用viewmodel的任何其他属性。

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

例如。像下面的东西。

<StackPanel DataContext={Binding CurrentUser}>

<control:RestrictedBox Value="{Binding UserName}"

 Type="Text" />

</StackPanel>

可能会帮助你......