Windows Vista Forums

TemplateBinding with Image Source Property
  1. #1


    HolaMan Guest

    TemplateBinding with Image Source Property

    Hi~~


    I want to binding a image from dependency property of the control
    I use template binding to bind a path which is string-type


    <Style x:Key="{x:Type local:BitmapButton}" TargetType="{x:Type
    local:BitmapButton}">
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type local:BitmapButton}">
    <Grid x:Name="Grid" >
    <ColumnDefinition/>
    <RowDefinition/>
    <Image Source=
    "{TemplateBinding local:BitmapButton.ImageDefault} Width="Auto"
    Height="Auto"/>
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ It
    doesn't work

    <TextBlock x:Name="ImagePath"
    Text="{TemplateBinding
    local:BitmapButton.ImageDefault}"/>

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it works
    <ContentPresenter ...
    </Grid>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>



    I have no idea why Image does not work but TextBlock's Text work.
    Any suggestions? :-(

    Thanks

    HolaMan


      My System SpecsSystem Spec

  2. #2


    Drew Marsh Guest

    Re: TemplateBinding with Image Source Property

    HolaMan wrote:

    > I have no idea why Image does not work but TextBlock's Text work. Any
    > suggestions? :-(


    It doesn't work because Image's Source property is not of type string, it
    is of type ImageSource. The only reason you can put a string there in XAML
    is because when XAML is parsed, the parser uses the converter architecture
    to translate the string into a real ImageSource. At runtime though, when
    the Binding is occuring, it's gotta be a real ImageSource instance. Therefore,
    if your datasource is providing you with a string, you'll need to implement
    an IValueConverter[1] to translate that string into a real ImageSource. My
    recommendation would be to simply use the ImageSourceConverter's[2] ConvertFrom
    method inside of your IValueConverter's Convert implementation.

    HTH,
    Drew

    [1] http://windowssdk.msdn.microsoft.com...eConverter.asp
    [2] http://windowssdk.msdn.microsoft.com...eConverter.asp
    ___________________________________
    Drew Marsh
    Chief Software Architect
    Mimeo.com, Inc. - http://www.mimeo.com
    Microsoft C# / WPF MVP
    Weblog - http://blog.hackedbrain.com/



      My System SpecsSystem Spec

  3. #3


    HolaMan Guest

    Re: TemplateBinding with Image Source Property

    Thanks for your reply. I will try it.



      My System SpecsSystem Spec

TemplateBinding with Image Source Property problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
XamlWriter.Save breaking Image.Source Curtis Avalon 0 21 Nov 2007
Custom Control Source Property - Required Attributes Karl 140.6 Avalon 0 07 Aug 2007
WPF Image source real size? JN1974 Avalon 3 12 Apr 2007
'Default' source for an Image thelemmings@gmail.com Avalon 1 11 Jan 2007
Set BorderThickness.Top Property in Trigger with TemplateBinding Thomas Mueller Avalon 1 31 May 2006