Ahad aghapour
Ahad aghapour

Reputation: 2553

WPF Dependency Property for Image source with IValueConverter

I intend to create Usercontrol with boolean dependency property called IsPowerOn, When I change it True the PowerOn image load to Image.source and when I set IsPowerOn to Fals, the PowerOff image load to Image.source.

Here is my UserControl:

<UserControl x:Class="...UcPower"
         ...
<UserControl.Resources>
    <local:PowerBoolean2Image x:Key="PowerBoolean2Image"/>
</UserControl.Resources>
<Grid>
    <Image x:Name="imgPower" Source="{Binding Source, Converter={StaticResource PowerBoolean2Image}, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UcPower}}}"  />
</Grid>

And Code behind:

    public static readonly DependencyProperty IsPowerOnProperty = DependencyProperty.Register("IsPowerOn", typeof(bool), typeof(UcPower),
     new FrameworkPropertyMetadata(false) { BindsTwoWayByDefault = true });

    public bool IsPowerOn
    {
        get
        {
            return (bool)GetValue(IsPowerOnProperty);
        }
        set
        {
            SetValue(IsPowerOnProperty, value);
        }
    }

And IValueConverter:

    public class PowerBoolean2Image : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is bool))
        {
            return null;
        }

        if (value.Equals(true))
        {
            // Power On
            return new BitmapImage(new Uri("pack://application:,,,/Resources/Power-On.png"));
        }
        else
        {
            // Power Off
            return new BitmapImage(new Uri("pack://application:,,,/Resources/Power-Off.png"));
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

But it doesn't work I expect, whats the wrong with me?

Upvotes: 1

Views: 789

Answers (2)

Ahad aghapour
Ahad aghapour

Reputation: 2553

When I use this code in IValueConverter I get Error: IOException: Cannot locate resource 'resources/power-on.png'. and cant see my form in design mode:

Uri("pack://application:,,,/Resources/Power-On.png")

But I can use Assembly name to solve the problem like this code:

Uri("pack://application:,,,/ReferencedAssembly;component/Resources/Power-On.png")

Upvotes: 0

Clemens
Clemens

Reputation: 128013

You should bind to the IsPowerOn property:

<Image Source="{Binding IsPowerOn, ...}" />

instead of

<Image Source="{Binding Source, ...}" />

Besides that, the expression if (value.Equals(true)) looks rather strange. You could replace that by

if ((bool)value)
{
    return new BitmapImage(new Uri("pack://application:,,,/Resources/Power-On.png"));
}

return new BitmapImage(new Uri("pack://application:,,,/Resources/Power-Off.png"));

or shorter:

return (bool)value
    ? new BitmapImage(new Uri("pack://application:,,,/Resources/Power-On.png"))
    : new BitmapImage(new Uri("pack://application:,,,/Resources/Power-Off.png"));

Upvotes: 2

Related Questions