开发者

Style Control Template as Image

I have two projects, the main project with the window and then another as ResourceLibrary. Inside Resourcelibrary I have a controltemplate called logo. I want to access the controltemplate and set it as an image in my main window. The xaml markup inside the controltemplate is an export from an image into paths to create the image. How can I do this, I've reference resource paths in app.xaml but still nothing shows up. Anybody know of an example that might demonstrate this?

In MainWindow

<Image Grid.Row="2" Grid.Column="2" DataContext="{DynamicResource ResourceKey=Logo}" />

In ResourceLibrary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:my="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:ResourceLibrary">
<ControlTemplate x:Key="Logo">
path stuff
</ControlTemplate>
</ResourceDictionary

In App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDicti开发者_如何学运维onary.MergedDictionaries>
            <ResourceDictionary Source="/ResourceLibrary;component/User Controls/Logo.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

FIXED I used a canvas background and set the resource to hit a visualbrush instead, this works.


Code below is not tried and tested... its just for your guidance...

  1. I dont understand why would you need a ControlTemplate for this. Expose that path itself as a resource from your the resource dictionary.

       <Path x:Key="MyImagePath"
             Fill="..."
             Stretch="..."
             Stroke="..."
             StrokeThickness="..."
             Data="...">
            ...
        </Path>
    
  2. Then use converter to host this path as an ImageSource.

    <Image Source="{Binding Source={StaticResource MyImagePath},
                            BindsDirectlyToSource=True,
                            Converter={StaticResource PathToImageConverter}}" />
    
  3. Use this logic to draw a geometry path into an Image, in the PathToImageConverter class .....

    public object Convert(
                 object value,
                 Type targetType,
                 object parameter,
                 System.Globalization.CultureInfo culture)
    {
        Path MyPath = value as Path; 
        var dGroup = new DrawingGroup();
        using (DrawingContext dc = dGroup.Open())
        {
            dc.DrawGeometry(
                 MyPath.Fill,
                 new Pen(MyPath.Stroke, MyPath.StrokeThickness),
                 MyPath.Data);
        }
    
        return new DrawingImage(dGroup); 
    }
    

Let me know if this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜