简体   繁体   English

基于属性值与DrawingImage的图像源绑定

[英]Image source binding with DrawingImage based on property value

I have declared DrawingImage data in a repository file as mentioned below. 我已在存储库文件中声明了DrawingImage数据,如下所述。

<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. -> MainWindow类文件中有一个字符串属性。

    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. ->在单击按钮时,我正在为ImgSource属性分配值。

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 如果ImgSource =“ low”,则在窗口中定义的图像应设置在资源字典中定义的Arrow

The built-in automatic type conversion from string to ImageSource does not magically work for your DrawingImage resources. 从string到ImageSource的内置自动类型转换对您的DrawingImage资源没有神奇的作用。

Change the type of your ImgSource property from string to ImageSource 将ImgSource属性的类型从字符串更改为ImageSource

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

and manually lookup the resource: 并手动查找资源:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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