与“用户控件"的动态绑定不起作用,因为“静态"在Silverlight和MVVM中起作用 [英] Dynamic binding with User Control does not work as Static is working in Silverlight and 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
推荐答案
发生这种情况是因为您将
UserControl
的DataContext
设置为自身.因此,当应用绑定时,它将在UserControl
中搜索UserName
属性(并且该属性不存在).
That happens because you set the
DataContext
of theUserControl
to itself. So, when the binding is applied, it searches theUserName
property in theUserControl
(and the property doesn''t exist).
您可以通过给UserControl
命名来绑定到UserControl
的属性,并使用Binding
的ElementName
属性,如下所示:
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屋!