RadioButton ContentStringFormat不起作用

时间:2014-02-14 17:09:57

标签: c# wpf c#-4.0 wpf-4.0

我一直在努力让下面的代码工作。绑定有效但第一个中的ContentStringFormat或第二个中的StringFormat似乎不起作用。

<RadioButton Content="{Binding ClientCode}" ContentStringFormat="{}{0} copy"
             IsChecked="{Binding Path= FilterType,
                 Converter={StaticResource EBConverter}, 
                 ConverterParameter={x:Static wordMerge:FilterType.ClientCopy}}" 
             Width="90"/>

上面的代码只返回绑定值,说“ABC”,但我期待“ABC copy”

 <RadioButton IsChecked="{Binding Path= FilterType,
                 Converter={StaticResource EBConverter},
                 ConverterParameter={x:Static wordMerge:FilterType.ClientCopy}}" 
              Width="90">
    <RadioButton.Content>
        <TextBlock Text="{Binding ClientCode, StringFormat={}{0} copy}"/>
     </RadioButton.Content>
 </RadioButton>

上面的代码没有返回任何绑定值。

更新 在两种情况下,在设计时或运行时都不会显示字符串副本。

2 个答案:

答案 0 :(得分:4)

试试这个

StringFormat='{}copy {0}'}"

测试

<TextBlock Text="{Binding Path=Str, Mode=OneWay, StringFormat='{}{0} copy'}"/>   

@Kiru它对我有用

<RadioButton Content="{Binding Path=Str, Mode=OneWay}"  ContentStringFormat='{}{0} copy'/>
<RadioButton>
    <TextBlock Text="{Binding Path=Str, Mode=OneWay, StringFormat='{}{0} copy'}"/>
</RadioButton>

答案 1 :(得分:1)

它不适合我的原因是因为我为RadioButton定义了自己的模板,但没有指定ContentStringFormat绑定。

<TextBlock 
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    Foreground="{TemplateBinding Foreground}">
    <InlineUIContainer><ContentPresenter ContentStringFormat="{TemplateBinding ContentStringFormat}"/></InlineUIContainer>
</TextBlock>
相关问题