none
StringFormat in TemplateBinding

    Question

  • Hi,

    I Want to bind a Value in Textbox using TemplateBinding.

    But i want to format the string before i bind the value. 

    Text="{TemplateBinding Tag, StringFormat={}}"

    But it is throughing error that TemplateBindingExtension doesnot have StringFormat property

    Is it possible to Format the string before Binding to it?
     
    Thank in Advance.
    Tuesday, March 15, 2011 12:55 PM

Answers

  • Hi,

    Thanks for your reply,

    I did a small work around for it,  and it worked fine,

    Here is what i did:

    DataContext="{TemplateBinding Value}" 
    Tag="{Binding Converter={StaticResource FormatConverter},ConverterParameter=\{0:n0\}}"

    and in converter class,

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               if (parameter != null)
               {
                   string formatterString = parameter.ToString();

                   if (!string.IsNullOrEmpty(formatterString))
                   {
                       return string.Format(culture, formatterString, value);
                   }
               }

               return value.ToString();
           }

    so that solves my problem, i can even use any number of formatting now.


    Wednesday, March 16, 2011 8:10 AM

All replies

  • A ValueConverter could do the formatting.

    Tuesday, March 15, 2011 1:40 PM
  • You can't apply Converter and StringFormat on TemplateBinding,  but here is a workaround.

    http://forums.silverlight.net/forums/p/38430/255932.aspx

    Tuesday, March 15, 2011 2:03 PM
  • Hi,

    Thanks for your reply,

    I did a small work around for it,  and it worked fine,

    Here is what i did:

    DataContext="{TemplateBinding Value}" 
    Tag="{Binding Converter={StaticResource FormatConverter},ConverterParameter=\{0:n0\}}"

    and in converter class,

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               if (parameter != null)
               {
                   string formatterString = parameter.ToString();

                   if (!string.IsNullOrEmpty(formatterString))
                   {
                       return string.Format(culture, formatterString, value);
                   }
               }

               return value.ToString();
           }

    so that solves my problem, i can even use any number of formatting now.


    Wednesday, March 16, 2011 8:10 AM
  • You could also use the RelativeSource xaml extension, something like this:

    Text="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}, StringFormat={}}"

    • Proposed as answer by scuba6388 Wednesday, April 10, 2013 2:04 PM
    Wednesday, March 16, 2011 8:23 AM
  • Thanks to suggest link by FuryDiamond I tried this XAML:

    <TextBlock DataContext="{TemplateBinding IndependentValue}" Text="{Binding StringFormat='\{0:MM/dd HH:mm\} '}"/>
    
    and work very fine!
    Got the value from Template and format it with the format string, without using any converter.

    Thanks to all.
    Davide.


    PS: This is the full code that worked for me. My objective was to create ToolTips with on a ColumnChart DataPoint:
     <Style x:Key="dataPointStyle" TargetType="chartingToolkit:ColumnDataPoint">
                    <Setter Property="BorderBrush" Value="Black"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="IsTabStop" Value="False"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="chartingToolkit:ColumnDataPoint">
                                <Border
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Opacity="0"
                            x:Name="Root">
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="CommonStates">
                                            <VisualStateGroup.Transitions>
                                                <VisualTransition GeneratedDuration="0:0:0.1"/>
                                            </VisualStateGroup.Transitions>
                                            <VisualState x:Name="Normal"/>
                                            <VisualState x:Name="MouseOver">
                                                <Storyboard>
                                                    <DoubleAnimation
                                                Storyboard.TargetName="MouseOverHighlight"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0.6"
                                                Duration="0"/>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                        <VisualStateGroup x:Name="SelectionStates">
                                            <VisualStateGroup.Transitions>
                                                <VisualTransition GeneratedDuration="0:0:0.1"/>
                                            </VisualStateGroup.Transitions>
                                            <VisualState x:Name="Unselected"/>
                                            <VisualState x:Name="Selected">
                                                <Storyboard>
                                                    <DoubleAnimation
                                                Storyboard.TargetName="SelectionHighlight"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0.6"
                                                Duration="0"/>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                        <VisualStateGroup x:Name="RevealStates">
                                            <VisualStateGroup.Transitions>
                                                <VisualTransition GeneratedDuration="0:0:0.5"/>
                                            </VisualStateGroup.Transitions>
                                            <VisualState x:Name="Shown">
                                                <Storyboard>
                                                    <DoubleAnimation
                                                Storyboard.TargetName="Root"
                                                Storyboard.TargetProperty="Opacity"
                                                To="1"
                                                Duration="0"/>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="Hidden">
                                                <Storyboard>
                                                    <DoubleAnimation
                                                Storyboard.TargetName="Root"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0"
                                                Duration="0"/>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                    <Grid
                                Background="{TemplateBinding Background}">
                                        <Rectangle>
                                            <Rectangle.Fill>
                                                <LinearGradientBrush>
                                                    <GradientStop
                                                Color="#77ffffff"
                                                Offset="0"/>
                                                    <GradientStop
                                                Color="#00ffffff"
                                                Offset="1"/>
                                                </LinearGradientBrush>
                                            </Rectangle.Fill>
                                        </Rectangle>
                                        <Border
                                    BorderBrush="#ccffffff"
                                    BorderThickness="0">
                                            <Border
                                        BorderBrush="#77ffffff"
                                        BorderThickness="1"/>
                                        </Border>
                                        <Rectangle x:Name="SelectionHighlight" Fill="Red" Opacity="0"/>
                                        <Rectangle x:Name="MouseOverHighlight" Fill="White" Opacity="0"/>
                                    </Grid>
                                    <ToolTipService.ToolTip>
                                        <StackPanel Margin="2,2,2,2">
                                            <TextBlock DataContext="{TemplateBinding IndependentValue}" Text="{Binding StringFormat='\{0:MM/dd HH:mm\} '}"/>
                                            <TextBlock  DataContext="{TemplateBinding DependentValue}" Text="{Binding StringFormat='\{0:#,##0.00\} '}"/>
                                            <TextBlock Text="{Binding Path=um}"/>
                                        </StackPanel>
                                    </ToolTipService.ToolTip>
                                    <!--<ToolTipService.ToolTip>
                                        <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                                    </ToolTipService.ToolTip>-->
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>


    
    

     

    
    

    Saturday, April 14, 2012 8:21 AM