Dependency property not working trying to set style

I am trying to customize my own style for my newly created usercontrol, but I get an error: "Unable to convert the value in the Property attribute to an object of type" System.Windows.DependencyProperty ".

I thought I set Dependency properties, but it didn't seem like that, so I did some research and added:

   public static readonly DependencyProperty ImageSourceProperty = 
   DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image));

to do the following: - MyButton.Xaml.Cs -

    namespace Client.Usercontrols
{
    public partial class MyButton : UserControl
    {
        public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image));

        public MyButton()
        {
            InitializeComponent();            
        }


        public event RoutedEventHandler Click;

        void onButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.Click != null)
                this.Click(this, e);
        }

        BitmapSource _imageSource;
        public BitmapSource ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                tehImage.Source = _imageSource;
            }
        }
    }
}

This, unfortunately, does not work. I also tried this:

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(MyButton.ImageSourceProperty); }
    set
    {
        SetValue(ImageSourceProperty, value);
    }
}

But this did not work, and the image was not shown and created the same error as mentioned earlier.

Any ideas? Regards, Cohan.

- MyButton.Xaml -

<UserControl x:Class="Client.Usercontrols.MyButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">

        <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
            <Grid>
                <Image Name="tehImage" Source="{Binding ImageSource}" />
                <TextBlock Name="tehText" Text="{Binding Text}" Style="{DynamicResource ButtonText}" />
            </Grid>
        </Border>

    </Button>
</UserControl>

- MYButton style -

<Style TargetType="{x:Type my:MyButton}" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type my:MyButton}">
                <ContentPresenter />
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="ImageSource" Value="../Images/Disabled.png" />                        
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
+3
4

( ):

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(ImageSourceProperty); }
    set { SetValue(ImageSourceProperty, value); }
}

/* Using a DependencyProperty as the backing store for ImageSource. 
   This enables animation, styling, binding, etc... */

public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", 
            typeof(BitmapSource), 
            typeof(MyButton), 
            new UIPropertyMetadata(null)
        );

, ImageSource , "tehImage". PropertyChangedCallback... , , , .

, :

public BitmapSource ImageSource
{
    get { return (BitmapSource)GetValue(ImageSourceProperty); }
    set { SetValue(ImageSourceProperty, value); }
}

/* Using a DependencyProperty as the backing store for ImageSource.  
   This enables animation, styling, binding, etc... */

public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", 
            typeof(BitmapSource), typeof(MyButton), 
            new UIPropertyMetadata(null, 
                ImageSource_PropertyChanged
            )
        );

private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    ((MyButton)source).tehImage.ImageSource = (ImageSource)e.NewValue
}

, ( ).

+3

, , , Image, UserControl. :

public static readonly DependencyProperty ImageSourceProperty = 
            DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(MyButton));

, XAML.

+4

DataContext UserControl:

public MyButton()
{
    InitializeComponent();
    DataContext = this;
}

, ( DataContext , ), XAML:

<UserControl x:Class="Client.Usercontrols.MyButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    x:Name="MyControl">


    <Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">

        <Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
            <Grid>
                <Image Name="tehImage" Source="{Binding ElementName=MyControl, Path=ImageSource}" />
                <TextBlock Name="tehText" Text="{Binding ElementName=MyControl, Path=Text}" Style="{DynamicResource ButtonText}" />
            </Grid>
        </Border>

    </Button>
</UserControl>
+1

, , BitmapSouce. (-, ) - Uri.

( ):

ImageSourceProperty = DependencyProperty.Register(
    "ImageSource", typeof (Uri), typeof (MyButton),
    new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));

:

public Uri ImageSource
{
    get
    {
           return (Uri)GetValue(ImageSourceProperty);
    }
    set
    {
           SetValue(ImageSourceProperty, value);
    }
}

:

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    MyButton hsb = (MyButton)sender;

    Image image = hsb.tehImage;
    image.Source = new BitmapImage((Uri) e.NewValue);
}
0

Source: https://habr.com/ru/post/1720549/


All Articles