我有一个带有图像和标签的自定义 UserControl,它们都是在设计时在 XAML 中设置的,如下所示:<controls:HomeBarButton Icon="/SuCo;component/Resources/music.png" Text="music"/>
当控件只有一个图标时,它看起来很好。当我添加 Text 属性时,图标在设计和运行时都会消失,并且文本标签会忽略 UserControl 中设置的格式,并且当标签居中时在控件的左上角只是黑色。
相关的用户控件 XAML:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Image x:Name="icon" Width="102" Height="102" VerticalAlignment="Stretch" Source="{Binding Icon}"/>
<Label x:Name="label" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" Foreground="White" FontFamily="Calibri" FontSize="24" Padding="0" Content="{Binding Text}"></Label>
</StackPanel>
代码隐藏:
public ImageSource Icon
{
get { return (ImageSource)this.GetValue(IconProperty); }
set { this.SetValue(IconProperty, value); }
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(HomeBarButton), new FrameworkPropertyMetadata(OnIconChanged));
private static void OnIconChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
dependencyObject.SetValue(Image.SourceProperty, e.NewValue);
}
public string Text
{
get { return (string)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(HomeBarButton), new FrameworkPropertyMetadata(OnTextChanged));
private static void OnTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
dependencyObject.SetValue(Label.ContentProperty, e.NewValue);
}
我究竟做错了什么?:(