Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

TemplateBinding of 'value' not working RSS

5 replies

Last post Oct 23, 2008 04:36 AM by Jonathan Shen – MSFT

(0)
  • damonpayne

    damonpayne

    Member

    324 Points

    82 Posts

    TemplateBinding of 'value' not working

    Oct 16, 2008 06:46 PM | LINK

    I have a control template for a ProgressBar.  The following should show the curent Value property in a TextBlock but does not:

    <TextBlock Height="15" HorizontalAlignment="Right" Margin="0,12,10,0" VerticalAlignment="Top" Width="136" Text="{TemplateBinding Value}" TextWrapping="Wrap"/>

    There is no binding validation error being thrown, and the ValueChanged event does fire.  What would prevent a TemplateBinding from working?

    -Damon
    http://www.damonpayne.com/
  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

    Re: TemplateBinding of 'value' not working

    Oct 22, 2008 09:41 AM | LINK

    Hi Damonpayne,

    Currently, integer/double type is not supported in your condition by default. We need to convert it to string manually.  However, we can use ContentControl. Thanks for your post.

    Best regards,

    Jonathan

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
  • andulvar

    andulvar

    Member

    181 Points

    129 Posts

    Re: TemplateBinding of 'value' not working

    Oct 22, 2008 11:09 AM | LINK

    I haven't written code to specifically test this, but try instead:

    <TextBlock DataContext="{TemplateBinding Value}" Height="15" HorizontalAlignment="Right" Margin="0,12,10,0" VerticalAlignment="Top" Width="136" Text="{Binding Converter={StaticResource Double2String}, ConverterParameter=0.000}" TextWrapping="Wrap"/>

    Now create a Double2String converter function like this:

        public class DoubleToStringConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    if (value == null)
    return "<null>";
    else if (parameter != null)
    return ((Double)value).ToString(parameter as string);
    else
    return
    ((Double)value).ToString();
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    try
    {
    return Double.Parse((String)value);
    }
    catch
    {
    return 0;
    }
    }
    }

    And in your <UserControl.Resources> block, add a line like this:

    <local:DoubleToStringConverter x:Key="Double2String"/>

  • damonpayne

    damonpayne

    Member

    324 Points

    82 Posts

    Re: TemplateBinding of 'value' not working

    Oct 22, 2008 07:13 PM | LINK

    Jonathan,

    I posted the simplest part of the example.  There is also a Path (think of a speedometer) on the control template.  The Rotate angle of the Path is also bound to Value and also not working.  Are you saying integer/double is not supported anywhere or only for TextBlock?

    -Damon
    http://www.damonpayne.com/
  • andulvar

    andulvar

    Member

    181 Points

    129 Posts

    Re: Re: TemplateBinding of 'value' not working

    Oct 23, 2008 12:25 AM | LINK

    I use int/double in TextBox and several other elements just fine.  The binding works using the example I posted above.  I think you may have some luck with that.  A couple of notes:

    You MUST use the converter, or it will silently fail

    You MUST use the {TemplateBinding ...} in the template, then use {Binding} where you want the data to change.  If you just use TemplateBinding directly, it will fail.  If you try to use Binding with a path, it should work but I have had mixed success.

     

  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

    Re: TemplateBinding of 'value' not working

    Oct 23, 2008 04:36 AM | LINK

    Hi Damonpayne,

    damonpayne

    Are you saying integer/double is not supported anywhere or only for TextBlock?

    ContentControl will convert int/double to string automatically, so it works.  We suggest that you can use a converter for more flexibility.

    Best regards,

    Jonathan

     

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework