我已经创建了包含TextBox和PasswordBox的用户控件。 RestrictedBox.xaml无法从主视图绑定UserControl
<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,这意味着它会自动隐藏密码箱或其他文本框。
任何帮助,将不胜感激!
感谢, Imdadhusen
很好的问题... – 2012-02-27 13:25:23
感谢萨加尔拉瓦尔。 – imdadhusen 2012-02-27 13:26:35