を使用しViewbox
て、WPFビューに動的にバインドする一連のアイコンを作成しています。
リソース名にバインドし、を使用してConverter
リソース名をに変換していImageSource
ます。
リソースがである場合の方法は知っていますが、?Path
で行う方法はViewbox
?
これは、リソースが、の場合、リソース名を:に変換する方法Path
です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>