当我为孩子分配值时,我的CustomControl会更改其子女的可见性。这是我的代码
<UserControl x:Class="CrossProjectUserControls.controls.OnOffSwitch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CrossProjectUserControls.controls"
mc:Ignorable="d" Background="White" Loaded="UserControl_Loaded">
<Grid>
<Image x:Name="imgSwitch" HorizontalAlignment="Left" MaxHeight="30" MinWidth="80" PreviewMouseLeftButtonDown="imgSwitch_PreviewMouseLeftButtonDown" Source="/CrossProjectUserControls;component/res/icons/onoffswitch/switch_off_1_V3_100x40.png" Margin="3,3,0,3"/>
<Label x:Name="lblText" Content="{Binding Text}" Margin="83,3,5,5" VerticalAlignment="Center" FontFamily="Arial" FontSize="16" Background="#FF633C3C"/>
</Grid>
public partial class OnOffSwitch : UserControl {
private BitmapImage bmpOn = new BitmapImage(new Uri(@"/MentorPlus;component/res/icons/onoffswitch/switch_off_1_V3_100x40.png", UriKind.Relative));
private BitmapImage bmpOff = new BitmapImage(new Uri(@"/MentorPlus;component/res/icons/onoffswitch/switch_on_1_V3_100x40.png", UriKind.Relative));
private bool isOn = false;
private string text = "Debug";
public bool IsOn
{
get { return isOn; }
set
{
isOn = value;
ReloadUI();
}
}
public string Text
{
get { return text; }
set { text = value; lblText.Content = text; }
}
public static readonly RoutedEvent SwitchEvent = EventManager.RegisterRoutedEvent(
"Switched", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(OnOffSwitch));
public OnOffSwitch() {
InitializeComponent();
}
private void InitializeControls() {
//this.lblText.Content = text;
//this.imgSwitch.Source = bmpOff;
}
public void Switch() {
IsOn = !IsOn;
}
private void ReloadUI() {
if (this.IsOn == true) {
this.imgSwitch.Source = bmpOn;
} else if (this.IsOn == false) {
this.imgSwitch.Source = bmpOff;
}
}
public event RoutedEventHandler Switched
{
add { AddHandler(SwitchEvent, value); }
remove { RemoveHandler(SwitchEvent, value); }
}
void RaiseSwitchEvent() {
RoutedEventArgs newEventArgs = new RoutedEventArgs(OnOffSwitch.SwitchEvent);
RaiseEvent(newEventArgs);
}
#region Events
private void imgSwitch_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
Switch();
RaiseSwitchEvent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
InitializeControls();
}
#endregion
}
因此,如果我为其指定了一个字符串,那么Label就完全隐藏了 this.lblText.Content =&#34; Test&#34; 。我有什么遗漏或做错了吗?
感谢您的每一次帮助