我正在使用 aViewbox
创建一组我将动态绑定到 WPF 视图的图标。
我绑定到资源名称并使用 aConverter
将资源名称转换为ImageSource
.
如果资源是 a Path
,我知道该怎么做,但是如何使用 aViewbox
呢?
Path
如果资源是 a ,这就是我将资源名称转换为 a的方式ImageSource
:
public class ResourceNameToImageSourceConverter : BaseValueConverter {
protected override ImageSource Convert(string value, System.Globalization.CultureInfo culture) {
var resource = new ResourceDictionary();
resource.Source = new Uri("pack://application:,,,/MyAssembly;component/MyResourceFolder/ImageResources.xaml", UriKind.Absolute);
var path = resource[value] as Path;
if (path != null) {
var geometry = path.Data;
var geometryDrawing = new GeometryDrawing();
geometryDrawing.Geometry = geometry;
var drawingImage = new DrawingImage(geometryDrawing);
geometryDrawing.Brush = path.Fill;
geometryDrawing.Pen = new Pen();
drawingImage.Freeze();
return drawingImage;
} else {
return null;
}
}
}
And this is what the Viewbox declaration looks like.
<Viewbox>
<Viewbox>
<Grid>
<Path>
...
</Path>
<Path>
...
</Path>
<Path>
...
</Path>
<Rectangle>
...
</Rectangle>
</Grid>
</Viewbox>
</Viewbox>