简体   繁体   中英

Image source binding with DrawingImage based on property value

I have declared DrawingImage data in a repository file as mentioned below.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:DynamicImageSourceFromResourceDIctionary">
    <DrawingImage x:Key="low">
        <DrawingImage.Drawing>
            <GeometryDrawing Geometry="M4.5,84.6l224.9,374.8c12.1,20.1,41.2,20.1,53.3,0L507.5,84.6c12.4-20.7-2.5-47.1-26.7-47.1H31.1
            C7,37.5-8,63.9,4.5,84.6z" Brush="#003854">
            </GeometryDrawing>
        </DrawingImage.Drawing>
    </DrawingImage>

    <DrawingImage x:Key="high">
        <DrawingImage.Drawing>
            <GeometryDrawing Geometry="M507.521,427.394L282.655,52.617c-12.074-20.122-41.237-20.122-53.311,0L4.479,427.394    c-12.433,20.72,2.493,47.08,26.655,47.08h449.732C505.029,474.474,519.955,448.114,507.521,427.394z" Brush="#003854">
            </GeometryDrawing>
        </DrawingImage.Drawing>
    </DrawingImage>
</ResourceDictionary>

--> Now In main window There is an image and two buttons

<Grid>
        <Image x:Name="image" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Source="{Binding ImgSource}"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="36,137,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <Button x:Name="button_Copy" Content="Button" HorizontalAlignment="Left" Margin="137,136,0,0" VerticalAlignment="Top" Width="75" Click="button_Copy_Click"/>
    </Grid>

--> there is a string property in MainWindow class file.

    private string _imgSource;
    public string ImgSource
    {
        get { return _imgSource; }
        set { _imgSource = value; OnPropertyChanged("ImgSource"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propname)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propname));
        }
    }

--> On button click, I am assigning value to ImgSource property.

private void button_Click(object sender, RoutedEventArgs e)
        {
            ImgSource = "low";
        }

        private void button_Copy_Click(object sender, RoutedEventArgs e)
        {
            ImgSource = "high";
        }

--> Now i want to set Drawing Image like

if ImgSource = "low" then Image defined in window, should set down Arrow defined in resource dictionary

The built-in automatic type conversion from string to ImageSource does not magically work for your DrawingImage resources.

Change the type of your ImgSource property from string to ImageSource

public ImageSource ImgSource
{
    get { return _imgSource; }
    set { _imgSource = value; OnPropertyChanged("ImgSource"); }
}

and manually lookup the resource:

ImgSource = Application.Current.FindResource("low") as ImageSource;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM