在Content Presenter中的Textblock上应用Converter

时间:2014-04-08 08:58:11

标签: c# wpf ivalueconverter

我的Groupbox有一个自定义ControlTemplate:

<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>

如您所见,有一个转换器尝试使用以下转换器将标题文本转换为TitleCase:

[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();
    }
}

我遇到的问题是,当我调试代码时,代码执行完美,我进入转换器代码,它实际上将头文本转换为TitleCase。但是......当我查看应用程序时,标题不会反映转换器返回的内容。

知道为什么吗?

0 个答案:

没有答案