Răspuns Put XAML image in viewbox through converter

  • 6 august 2012 15:27
     
      Are cod

    New to WPF. I'm trying to supply an image defined in XAML based on a tag returned from a converter. Example:

    <Viewbox Margin="0,10" Height="50" MaxWidth="100" Grid.Column="2" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Viewbox.Child>
            <MultiBinding Converter="{StaticResource ConvertTypeToImage}">
                <Binding Path="Image.Type"></Binding>
                <Binding Path="Element.Type"></Binding>
            </MultiBinding>
        </Viewbox.Child>
            <Viewbox.LayoutTransform>
            <RotateTransform CenterX=".5" CenterY=".5" Angle="90" />
        </Viewbox.LayoutTransform>
    </Viewbox>
    The image XAML is included via App.xaml ResourceDictionary.MergedDictionaries and defines an x:Key value. Can I somehow just return the key from the converter to display the image? Do I need to return the full XAML of the image? What's the correct method?

Toate mesajele

  • 6 august 2012 15:48
     
     Răspuns Are cod

    Hello,

    Converter is last step of binding. Converter converts Image.Type and element.Type  value to UIElement because Child propery is UI element Converter must return UIElement or DepencyProperty.UnsetValue.

    So in you  converter code  you must locate and load resource. 

    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findresource.aspx

    If WPF is able to allow binding expression to convert than you can do the thing you want but its not possible.

    Regards.


  • 13 august 2012 10:00
    Moderator
     
     Răspuns

    Hi DAM,

    Sorry for that this is not possible, because we can't directly binding a control into your ViewBox. If you want to reuse a Image, there is number of ways to achieve your goal, you can binding the Image path to achieve your goal instead of bind image type and element type. Or you can also as what MDinc has mentioned, to use FindResource method, then add the image into ViewBox in code.

    Hope this helps.

    Have a nice day. 


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us