我已經創建了包含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