0

I have a custom ControlTemplate for my Groupbox:

<converters:StringCaseConverter x:Key="stringCaseConverter" />
<Style TargetType="GroupBox">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="GroupBox">
            <Border BorderBrush="#DEEBF3" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3" Margin="2,2,2,2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="0">
                        <Label Foreground="#3B3A35" FontSize="17" FontWeight="SemiBold" FontFamily="Segoe UI">
                            <ContentPresenter Margin="4,0,4,0" ContentSource="Header" RecognizesAccessKey="True">
                                <ContentPresenter.Resources>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Text" Value="{Binding Converter={StaticResource stringCaseConverter}, ConverterParameter='Title'}" />
                                    </Style>
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                        </Label>
                    </StackPanel>
                    <ContentPresenter Grid.Row="1" Margin="14" />
                </Grid>
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity=".5">
                        <GradientStop Offset="0" Color="#AFBCC7" />
                        <GradientStop Offset=".1" Color="#C3D6E4" />
                        <GradientStop Offset=".9" Color="#C6D8E2" />
                        <GradientStop Offset="1" Color="#B4C3CF" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

As you can see, there is a converter that tries to convert the header text to TitleCase using the following Converter:

[ValueConversion(typeof(string), typeof(string))]
public class StringCaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        var currentString = (string)value;
        var conversion = (string)parameter;

        if (conversion == "Base64")
        {
            byte[] txtByte = Encoding.Default.GetBytes(currentString);
            string outputString = System.Convert.ToBase64String(txtByte);
            return outputString;
        }

        if (conversion == "Upper")
            return currentString.ToUpper(culture);

        if (conversion == "Lower")
            return currentString.ToLower(culture);

        if (conversion == "Title")
        {
            TextInfo text = culture.TextInfo;
            return text.ToTitleCase(currentString);
        }

        return string.Empty;
    }

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

The problem I am having here is that when I debug the code, the code executes perfectly and I step into the converter code, where it actually converts the header text into TitleCase. BUT... when I look at the app, the header is not reflecting what the converter returned.

Any idea why?

stoic
  • 4,700
  • 13
  • 58
  • 88
  • Set Binding `Mode` as `TwoWay`. – Nikhil Agrawal Apr 08 '14 at 09:01
  • @Nikhil Agrawal, to set `Mode=TwoWay` I need to specify binding path on the TextBlock. I cannot do that as I am not binding to the TextBlock, I am binding on the ContentPresenter, and simply converting all textblock Text using the converter..... or am I wrong here? – stoic Apr 08 '14 at 09:06
  • 1
    My question to you is *why are you using the `ContentPresenter` in the first place?* If you want a `TextBlock`, then can't you just use a `TextBlock`? – Sheridan Apr 08 '14 at 11:32
  • @Sheridan, Good question. I am still learning WPF, but I also ended up using a label instead of a ContentPresenter. – stoic Apr 09 '14 at 07:17

0 Answers0