代码之家  ›  专栏  ›  技术社区  ›  Eric Smith

UserControl自定义依赖属性问题

  •  1
  • Eric Smith  · 技术社区  · 15 年前

    <controls:HomeBarButton Icon="/SuCo;component/Resources/music.png" Text="music"/>

    相关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);
        }
    

    我做错了什么(

    1 回复  |  直到 15 年前
        1
  •  0
  •   Muad'Dib    15 年前

    首先,我将该标签更改为TextBlock——您将使用Label将标签的文本与另一个控件关联。从您的代码来看,您似乎没有这样做,只想显示文本。要检查的另一件事是,文本是否显示在图标顶部。我猜这就是正在发生的事情。更改为文本块可能会解决此问题,如果没有,您可能应该手动设置文本块的高度和宽度。只是我的.02英镑。