【发布时间】:2012-02-27 11:46:57
【问题描述】:
我创建了包含 TextBox 和 PasswordBox 的用户控件。 RestrictedBox.xaml
<UserControl.Resources>
<Converters:BoolToVisibilityConverter x:Key="boolToVisConverter" />
<Converters:BoolToVisibilityConverter x:Key="boolToVisConverterReverse" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" Width="Auto">
<StackPanel Margin="5,5,5,5">
<TextBox Text="{Binding TextValue}" Visibility="{Binding IsTextBox,Converter={StaticResource boolToVisConverter}}" BorderBrush="Green" />
<PasswordBox Password="{Binding TextValue}" Visibility="{Binding IsTextBox,Converter={StaticResource boolToVisConverterReverse}}" BorderBrush="Red" />
</StackPanel>
</Grid>
RestrictedBox.xaml.cs
public partial class RestrictedBox : UserControl
{
public RestrictedBox()
{
InitializeComponent();
}
public string TextValue
{
get { return (string)GetValue(TextValueProperty); }
set { SetValue(TextValueProperty, value); }
}
public static readonly DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(RestrictedBox), new PropertyMetadata(default(string)));
public bool IsTextBox
{
get { return (bool)GetValue(IsTextBoxProperty); }
set { SetValue(IsTextBoxProperty, value); }
}
public static readonly DependencyProperty IsTextBoxProperty = DependencyProperty.Register("IsTextBox", typeof(bool), typeof(RestrictedBox), new PropertyMetadata(default(bool)));
}
现在我将上面的用户控件添加到我的 LoginView.xaml 页面
<control:RestrictedBox TextValue="Imdadhusen" IsTextBox="True" />
现在我运行应用程序,但 TextValue = "Imdadhusen" 没有与我的文本框绑定,并且第二个属性 IsTextBox 设置为 True,这意味着它将自动隐藏 Passwordbox else Textbox。
任何帮助将不胜感激!
谢谢, 伊姆达杜森
【问题讨论】:
标签: mvvm silverlight-4.0 user-controls prism-4