.net 连接字符串,而不是使用文本块堆栈

.net 连接字符串,而不是使用文本块堆栈,.net,wpf,datatemplate,.net,Wpf,Datatemplate,我想在WPF ItemsControl中显示客户对象的列表。我已为此创建了一个数据模板: <DataTemplate DataType="{x:Type myNameSpace:Customer}"> <StackPanel Orientation="Horizontal" Margin="10"> <CheckBox"></CheckBox> <TextBlock Te

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

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


您可能可以使用StringFormat属性(在.NET3.5SP1中)。并且可以找到有用的WPF绑定欺骗。 如果没有帮助,您可以为对象编写自己的ValueConverter或自定义属性

刚刚选中,您可以将StringFormat与多重绑定一起使用。在您的案例中,代码如下所示:

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

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


如果要用静态文本表示动态值,请尝试以下操作:

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


显示:234-334-5566(SSN)

请参见我在使用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 >

代替可以使用{}的空格,例如StringFormat=“{}{0}-{1}”还可以用反斜杠转义大括号:此外,关闭文本块丢失,所以总结一下评论:@PiRX如果我想显示'number'偶数如果“name”是空的-我该怎么做?@DasDas很遗憾,我已经好几年没有与WPF合作了,因此我无法帮助您回答这个问题。有趣的是,你很快就忘记了不再使用的东西。TextBlockLeftStyle的内容是什么?这是一种自定义样式,我必须将textblock向左对齐。这在这里没有意义。这是将绑定与字符串连接起来的最佳解决方案吗?你能解释一下优点吗?没有区别;10年前编写此答案时,Run不支持文本属性上的绑定!
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);
        }
    }
<TextBlock Text="{Binding IndividualSSN, StringFormat= '\{0\} (SSN)'}"/>
        <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 >