Связывание данных с пользовательским элементом управления не работает для источника фонового изображения кнопки - PullRequest
0 голосов
/ 14 января 2019

Я добавил пользовательский элемент управления в App.xml

<Grid>
    <Button Style="{StaticResource PlayerButtonsStyle}"
            Width="{Binding ElementName=UC, Path=ImageWidth}"
            Height="{Binding ElementName=UC, Path=ImageHeight}">
        <Button.Background>
            <ImageBrush ImageSource="{Binding ElementName=UC, Path=Image}" />
        </Button.Background>
    </Button>
</Grid>

В моем главном окне я добавляю следующее, чтобы добавить кнопку. И это не отображает изображение, которое я добавил в качестве ресурса.

<local:ImageButton HorizontalAlignment="Left"
                   Height="25"
                   Margin="474,430,0,0"
                   VerticalAlignment="Top"
                   Width="25"
                   Image="images/play.png">
</local:ImageButton>

Но если я это сделаю, будет отображаться следующее изображение, но это побеждает цель сделать его пользовательским элементом управления.

<local:ImageButton HorizontalAlignment="Left"
                   Height="25"
                   Margin="474,430,0,0"
                   VerticalAlignment="Top"
                   Width="25"
                   Image="images/forward.png">
    <local:ImageButton.Background>
        <ImageBrush ImageSource="images/play.png"/>
    </local:ImageButton.Background>
</local:ImageButton>

Вот пользовательский элемент управления

public partial class ImageButton : UserControl
{
    public ImageButton()
    {
        InitializeComponent();
    }

    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Image.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));

    public double ImageWidth
    {
        get { return (double)GetValue(ImageWidthProperty); }
        set { SetValue(ImageWidthProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ImageWidth.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageWidthProperty =
        DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton), new UIPropertyMetadata(16d));

    public double ImageHeight
    {
        get { return (double)GetValue(ImageHeightProperty); }
        set { SetValue(ImageHeightProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ImageHeight.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageHeightProperty =
        DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton), new UIPropertyMetadata(16d));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(ImageButton), new UIPropertyMetadata(""));
}

Определение стиля

<Style x:Key="PlayerButtonsStyle" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <!--<Setter Property="Margin" Value="5"/>-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Name="border" BorderThickness="1" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}" >
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="CornerRadius" Value="3" />
                            </Trigger>
                            <Trigger Property="Button.IsPressed" Value="True" >
                                <Setter TargetName="border" Property="BorderBrush" Value="Transparent" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Чего мне не хватает?

...