WPF Image控件Source属性绑定后台实现切换图片

发布于:2025-09-11 ⋅ 阅读:(18) ⋅ 点赞:(0)
public class ImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        

        try
        {
            var imagePath = new BitmapImage(new Uri("/Lithography;component/Themes/Images/action2.png", UriKind.RelativeOrAbsolute));
            var ret = (ActionType)value;
            if (ret == ActionType.ParallelAndWaitForActionToBeCompleted)
            {
                 imagePath = new BitmapImage(new Uri("/Lithography;component/Themes/Images/parallel2.png", UriKind.RelativeOrAbsolute));

            }
            else if (ret == ActionType.Sequence)
            {
                imagePath = new BitmapImage(new Uri("/Lithography;component/Themes/Images/order2.png", UriKind.RelativeOrAbsolute));

            }
           
            return imagePath;

            

            return null;
        }
        catch (Exception)
        {
            // 可以返回一个默认图片或者 null
            return null;
        }
    }

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

和在xaml中使用Source差不多:
 

 <Image
     Width="20"
     Height="15"
     VerticalAlignment="Center"
     Source="/Lithography;component/Themes/Images/MoveTo.png" />

-----------------------------------------------------------------------

图片看起来有锯齿可以在Image控件中这样设置:

 RenderOptions.BitmapScalingMode="HighQuality"
 RenderOptions.EdgeMode="Aliased"
 SnapsToDevicePixels="True"

前后效果:

是好了一点。


网站公告

今日签到

点亮在社区的每一天
去签到