.net 连接字符串而不是使用一堆 TextBlocks
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/541896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate strings instead of using a stack of TextBlocks
提问by Gerrie Schenck
I want to show a list of Customer objects in a WPF ItemsControl. I've created a DataTemplate for this:
我想在 WPF ItemsControl 中显示 Customer 对象的列表。我为此创建了一个 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>
So what I want basically is a simple list (with checkboxes) that contains NUMBER - NAME. Isn't there a way in which I can concat the number and name directly in the Binding part?
所以我想要的基本上是一个包含 NUMBER - NAME 的简单列表(带复选框)。有没有办法直接在绑定部分连接数字和名称?
回答by PiRX
There is StringFormat property (in .NET 3.5 SP1), which you probably can use. And usefull WPF binding cheat sheat can found here. If it doesn't help, you can allways write your own ValueConverter or custom property for your object.
您可能可以使用 StringFormat 属性(在 .NET 3.5 SP1 中)。可以在此处找到有用的 WPF 绑定秘籍。如果它没有帮助,您始终可以为您的对象编写自己的 ValueConverter 或自定义属性。
Just checked, you can use StringFormat with multibinding. In your case code will be something like this:
刚刚检查过,您可以将 StringFormat 与多重绑定一起使用。在您的情况下,代码将是这样的:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat=" {0} - {1}">
<Binding Path="Number"/>
<Binding Path="Name"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
I had to start format string with space, otherwise Visual Studio wouldn't build, but I think you will find way get around it :)
我必须用空格开始格式化字符串,否则 Visual Studio 不会构建,但我认为你会找到解决它的方法:)
Edit
The space is needed in the StringFormat to keep the parser from treating {0}as an actual binding. Other alternatives:
编辑
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}">
回答by redskull
In case you want to concat a dynamic value with a static text, try this:
如果您想使用静态文本连接动态值,请尝试以下操作:
<TextBlock Text="{Binding IndividualSSN, StringFormat= '\{0\} (SSN)'}"/>
Displays: 234-334-5566 (SSN)
显示器: 234-334-5566 (SSN)
回答by user3262530
See the following example I used in my code using Run class:
请参阅我在使用 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 >
回答by cz_dl
You can also use a bindable run. Useful stuff, especially if one wants to add some text formatting (colors, fontweight etc.).
您还可以使用可绑定运行。有用的东西,特别是如果你想添加一些文本格式(颜色、字体等)。
<TextBlock>
<something:BindableRun BoundText="{Binding Number}"/>
<Run Text=" - "/>
<something:BindableRun BoundText="{Binding Name}"/>
</TextBlock>
Here'san original class:
Hereare some additional improvements.
And that's all in one piece of code:
这是一个原始类:
这里有一些额外的改进。
这一切都在一段代码中:
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);
}
}

