【问题标题】:Concatenate strings instead of using a stack of TextBlocks连接字符串而不是使用一堆 TextBlocks
【发布时间】:2009-02-12 15:48:38
【问题描述】:

我想在 WPF ItemsControl 中显示客户对象的列表。我为此创建了一个 DataTemplate:

    <DataTemplate DataType="{x:Type myNameSpace:Customer}">
        <StackPanel Orientation="Horizontal" Margin="10">
            <CheckBox"></CheckBox>
            <TextBlock Text="{Binding Path=Number}"></TextBlock>
            <TextBlock Text=" - "></TextBlock>
            <TextBlock Text="{Binding Path=Name}"></TextBlock>
        </StackPanel>
    </DataTemplate>

所以我想要的基本上是一个包含 NUMBER - NAME 的简单列表(带有复选框)。有没有办法可以直接在绑定部分连接数字和名称?

【问题讨论】:

    标签: .net wpf datatemplate


    【解决方案1】:

    您可能可以使用 StringFormat 属性(在 .NET 3.5 SP1 中)。有用的 WPF 绑定备忘单可以找到here。 如果没有帮助,您始终可以为您的对象编写自己的 ValueConverter 或自定义属性。

    刚刚检查过,您可以将 StringFormat 与多重绑定一起使用。在您的情况下,代码将是这样的:

    <TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat=" {0} - {1}">
            <Binding Path="Number"/>
            <Binding Path="Name"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>
    

    我必须以空格开头格式字符串,否则 Visual Studio 将无法构建,但我认为您会找到解决方法 :)

    编辑
    StringFormat 中需要该空间以防止解析器将{0} 视为实际绑定。其他选择:

    <!-- use a space before the first format -->
    <MultiBinding StringFormat=" {0} - {1}">
    
    <!-- escape the formats -->
    <MultiBinding StringFormat="\{0\} - \{1\}">
    
    <!-- use {} before the first format -->
    <MultiBinding StringFormat="{}{0} - {1}">
    

    【讨论】:

    • 您可以使用 {} 来代替空间,例如StringFormat="{}{0} - {1}"
    • 你也可以用反斜杠转义大括号:
    • 另外,缺少关闭的 TextBlock,所以总结一下 cmets:
    • @PiRX 如果我想显示“数字”,即使“名称”为空 - 我该怎么做?
    • @DasDas 不幸的是,我无法帮助您解决问题,因为我已经有几年没有使用 WPF 了。有趣的是,您忘记不再使用的东西的速度有多快。
    【解决方案2】:

    如果您想将动态值与静态文本连接,请尝试以下操作:

    <TextBlock Text="{Binding IndividualSSN, StringFormat= '\{0\} (SSN)'}"/>
    

    显示:234-334-5566 (SSN)

    【讨论】:

      【解决方案3】:

      请参阅我在代码中使用 Run 类的以下示例:

              <TextBlock x:Name="..." Width="..." Height="..."
                  <Run Text="Area="/>
                  <Run Text="{Binding ...}"/>
                  <Run Text="sq.mm"/>
                  <LineBreak/>
                  <Run Text="Min Diameter="/>
                  <Run Text="{Binding...}"/>
                  <LineBreak/>
                  <Run Text="Max Diameter="/>
                  <Run Text="{Binding...}"/>
              </TextBlock >
      

      【讨论】:

        【解决方案4】:

        您也可以使用可绑定的运行。有用的东西,特别是如果你想添加一些文本格式(颜色、字体粗细等)。

        <TextBlock>
           <something:BindableRun BoundText="{Binding Number}"/>
           <Run Text=" - "/>
           <something:BindableRun BoundText="{Binding Name}"/>
        </TextBlock>
        

        Here's 原始类:
        Here 是一些额外的改进。
        而这一切都在一段代码中:

        public class BindableRun : Run
            {
                public static readonly DependencyProperty BoundTextProperty = DependencyProperty.Register("BoundText", typeof(string), typeof(BindableRun), new PropertyMetadata(new PropertyChangedCallback(BindableRun.onBoundTextChanged)));
        
                private static void onBoundTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    ((Run)d).Text = (string)e.NewValue;
                }
        
                public String BoundText
                {
                    get { return (string)GetValue(BoundTextProperty); }
                    set { SetValue(BoundTextProperty, value); }
                }
        
                public BindableRun()
                    : base()
                {
                    Binding b = new Binding("DataContext");
                    b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1);
                    this.SetBinding(DataContextProperty, b);
                }
            }
        

        【讨论】:

        • ?你能解释一下优点吗?
        • 没有区别; 10 年前编写此答案时,Run 不支持对 Text 属性的绑定!
        猜你喜欢
        • 1970-01-01
        • 2021-09-23
        • 2014-06-14
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 2017-05-19
        • 2014-07-31
        • 1970-01-01
        相关资源
        最近更新 更多