【问题标题】:Is it possible to add more characters after a binding in xaml?是否可以在 xaml 中绑定后添加更多字符?
【发布时间】:2011-06-24 06:15:22
【问题描述】:

我想知道一些事情,但找不到任何相关主题。我有以下绑定:

Content="{x:Static resx:Resource.Form_OtherOption_Description}"

这将在标签中放置一个字符串。我问自己是否可以在绑定之后添加一个“:”,而不是在代码中,只是在 xaml 中。标签代表类似于“名称:”的内容。但是添加“:”作为绑定的一部分不是一种选择。

编辑

我正在使用 3.5 版本

任何建议。

提前致谢。

【问题讨论】:

  • 如果您使用的是 3.5,您很可能需要使用 @H.B. 的解决方案。

标签: wpf xaml binding


【解决方案1】:

您可以通过以下方式完成此操作:

<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
                         StringFormat={}{0}:}" />

编辑: &lt;Label&gt;s Content 属性显然不尊重绑定的 StringFormat 属性。我发现已移至&lt;Label&gt; 上的ContentStringFormat 属性。

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
       ContentStringFormat="{}{0}:" />

【讨论】:

  • +1,看到你也找到了答案:) 所以ContentControlContentStringFormatHeaderedContentControlHeaderStringFormatItemsControlItemStringFormat。很高兴知道
  • 看来Label.Content 没有从绑定中拾取StringFormat,因为TargetType 不是string,而是object。在TextBlock.Text的情况下,目标类型是string,因此在绑定中使用。
  • 好地方 - 虽然你可以在标签内容中添加一个 TextBlock 不那么优雅,但这将是额外的开销。
【解决方案2】:

如果您使用的是 WPF 4.0,您也可以这样做:

<TextBlock>
       <Run Text="{Binding SomeLabel}"/>
       <Run Text=":"/>
</TextBlock>

这实际上将来自两个Run 标记的两个字符串连接起来并复制到TextBlock.Text 属性中!。

使用这种方法,您甚至可以绑定到 Presenter 中的不同属性,并将其显示在单个 TexBlock 中。看看这个优秀的例子:

Can we concat two properties in data binding?

【讨论】:

    【解决方案3】:

    您还可以将 MultiBinding 与 StringFormat 一起使用,例如:

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="ID {0} Name: {1} Age: {2}">
                <Binding Source="{x:Static resx:SomeResx.ID}"/>
                 <Binding Path="Name"/>
                 <Binding Path="Age"/>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
    

    您可以在内容控件 TextBlock TextBlock.Text 中使用它(抱歉,我无法在上面显示代码)

    【讨论】:

    • 根据上面的六个字母变量答案,标签使用 contentStringFormat。要使上述多重绑定与标签一起使用,请将 TextBlock 分配给其内容。
    【解决方案4】:

    是的,你可以。这里我在windows phone中绑定text(clouds.all)后添加“测试”。

    <TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
    

    【讨论】:

      【解决方案5】:

      试试 Binding 的属性StringFormat - 它可以非常简单地完成您想要的操作。

      【讨论】:

        【解决方案6】:

        如果你在进度条中使用标签,你可以这样使用:

        <Label x:Name="Progress" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Foreground="White" Opacity=".7" 
               Content="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}%">
        

        通过这种方式,您可以通过添加 % 来可视化进度条的值。

        【讨论】:

          【解决方案7】:

          您可以创建一个转换器,接收输入字符串并添加“:”。

          public class AddStringToStringConverter : IValueConverter
          {
              #region IValueConverter Members
          
              public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  string input = value as string;
                  string suffix = parameter as string;
          
                  return input + suffix;
              }
          
              public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  throw new NotImplementedException();
              }
          
              #endregion
          }
          

          Xaml:

          <Window.Resources>
              <local:AddStringToStringConverter x:Key="AddStringToStringConverter"/>
          </Window.Resources>
          ...
          <Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/>
          

          或者类似的东西。试过了,它至少对我的来源有用。

          如果ConverterParameter 中有空格等,您可以使用单引号来确保它不会被丢弃。

          编辑:哦,对了...是的...还有StringFormat,我以前从来不需要,呵呵呵呵...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-09-19
            • 1970-01-01
            • 2010-10-27
            • 1970-01-01
            • 2019-08-18
            • 1970-01-01
            • 2020-01-17
            • 2016-11-27
            相关资源
            最近更新 更多