1

画像とラベルを持つカスタム UserControl があり、どちらも XAML でデザイン時に次のように設定されます。<controls:HomeBarButton Icon="/SuCo;component/Resources/music.png" Text="music"/>

コントロールにアイコンだけがある場合、見た目は問題ありません。Text プロパティを追加すると、デザイン時と実行時の両方でアイコンが消え、テキスト ラベルは UserControl で設定された書式設定を無視し、ラベルが中央に配置されるとコントロールの左上隅が黒くなります。

関連する 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);
    }

私は何を間違っていますか?:(

4

1 に答える 1

0

まず、その Label を TextBlock に変更します。Label を使用して、ラベルのテキストを別のコントロールに関連付けます。あなたのコードから、これを行っておらず、テキストのみを表示したいようです。もう 1 つの確認事項は、テキストがアイコンの上に表示されているかどうかです。これが起こっていることだと思います。TextBlock に変更すると、これが修正される場合があります。そうでない場合は、TextBlock の高さと幅を手動で設定する必要があります。ちょうど私の.02の価値。

于 2009-03-26T20:17:38.053 に答える