views:

288

answers:

1

I used Mike Swanson's illustrator to xaml converter to convert some of my images to xaml. The convert creates a viewbox that contains the image. These viewboxes I made resource files in my program.

The code below shows what I'm trying to do: I have a viewmodel that has an enum variable called PrimaryWinding of type Windings. The values PrimD and PrimY of the enum select the respective PrimD and PrimY xaml files in the resources.

<UserControl.Resources>
    <DataTemplate x:Key="PrimTrafo" DataType="{x:Type l:Windings}">
        <Frame Source="{Binding}" x:Name="PART_Image" NavigationUIVisibility="Hidden">
            <Frame.LayoutTransform>
                <ScaleTransform ScaleX="0.5" ScaleY="0.5"/>
            </Frame.LayoutTransform>
        </Frame>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding}" Value="PrimD">
                <Setter TargetName="PART_Image" Property="Source" Value="Resources\PrimD.xaml" />
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="PrimY">
                <Setter TargetName="PART_Image" Property="Source" Value="Resources\PrimY.xaml" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</UserControl.Resources>

<!--The contentcontrol that holds the datatemplate defined above-->
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"></ColumnDefinition>
        <ColumnDefinition Width="2*"></ColumnDefinition>
        <ColumnDefinition Width="1*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ContentControl Grid.Column="0" Content="{Binding PrimaryWinding}" ContentTemplate="{StaticResource PrimTrafo}"/>
</Grid>

This code works. Only I can't resize the drawings to the size of the grid cell. I added the ScaleTransform class to resize the image.
Is a Frame the wrong class to hold the drawings?
Should I use the ScaleTransform class to resize the drawing to the size of the cell? And how can I do that dynamically?

A: 

I found a solution that worked, but I still don't like it very much. If I put my ContentControl in a Viewbox it resizes the content to the size of the grid cell. This also works for the code in my first message. I also tried another solution by building a converter that creates a viewbox based on an enum value.
I still hope someone knows a better solution?

<Viewbox Grid.Column="0" Stretch="Fill">
    <ContentControl Content="{Binding PrimaryWinding,  Converter={StaticResource viewboxConverter}}"/>
</Viewbox>

The code for the enum value to viewbox converter:

[ValueConversion(typeof(Windings), typeof(Viewbox))]
public sealed class WindingsToViewboxConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        try
        {
            string source=null;
            switch ((Windings)value)
            {
                case Windings.PrimD:
                    source = Properties.Resources.PrimD;
                    break;
                case Windings.PrimY:
                    source =Properties.Resources.PrimY;
                    break;
                default:
                    break;
            }
            Viewbox viewbox = (Viewbox)XamlReader.Parse(source);
            return viewbox;
        }
        catch
        {
            return new Viewbox();
        }
    }

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

The grid cell now has a viewbox containing a contentcontrol containing a viewbox, which I find a bit awkward.

Robert