wpf 是否可以在 xaml 中绑定后添加更多字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4947395/
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
Is it possible to add more characters after a binding in xaml?
提问by Terry
I was wondering something, and couldn't find any relevant topics. I have following binding :
我想知道一些事情,但找不到任何相关主题。我有以下绑定:
Content="{x:Static resx:Resource.Form_OtherOption_Description}"
This will place a string in a label. What i was asking myself is if i can add a ":" after that binding, not in code, just in xaml. The label represent something like "Name :". But adding the ":" as part of the binding is not an option.
这将在标签中放置一个字符串。我问自己是否可以在绑定之后添加一个“:”,而不是在代码中,而是在 xaml 中。标签表示类似“名称:”的内容。但是添加“:”作为绑定的一部分不是一个选项。
Edit
编辑
I'm working in 3.5 version
我正在使用 3.5 版本
Any suggestions.
有什么建议。
Thanks in advance.
提前致谢。
回答by user7116
You could accomplish this with something like:
您可以使用以下方法完成此操作:
<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
StringFormat={}{0}:}" />
Edit:<Label>
s Content
property does not respect the StringFormat
property of a binding apparently. Which I've found has been moved to the ContentStringFormat
property on the <Label>
.
编辑:<Label>
sContent
属性显然不尊重StringFormat
绑定的属性。我已经发现已经移动到ContentStringFormat
的财产<Label>
。
<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
ContentStringFormat="{}{0}:" />
回答by Nawaz
If you're using WPF 4.0, you could also do this:
如果您使用的是 WPF 4.0,您也可以这样做:
<TextBlock>
<Run Text="{Binding SomeLabel}"/>
<Run Text=":"/>
</TextBlock>
This actually concatenates the two strings coming from two Run
tag and copied into TextBlock.Text
property!.
这实际上将来自两个Run
标签的两个字符串连接起来并复制到TextBlock.Text
属性中!
Using this approach you can even bind to different properties in presenter, and display it in a single TexBlock
. See this excellent example:
使用这种方法,您甚至可以绑定到 Presenter 中的不同属性,并将其显示在单个TexBlock
. 看看这个很好的例子:
回答by Andrew
You can also use MultiBinding with StringFormat e.g:
您还可以将 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>
You can use this in a content control TextBlock TextBlock.Text (sorry I couldn't get the code to show up for this above)
您可以在内容控件 TextBlock TextBlock.Text 中使用它(抱歉,我无法在上面显示代码)
回答by reza.cse08
Yes you can. Here I add "testing" after binding text(clouds.all) in windows phone.
是的你可以。这里我在windows手机中绑定文本(clouds.all)后添加“测试”。
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
回答by Matěj Zábsky
Try Binding's property StringFormat- it can do very simply what you want.
尝试使用 Binding 的属性StringFormat- 它可以非常简单地完成您想要的操作。
回答by luka
if you use a label inside a progress bar you can use this way:
如果您在进度条内使用标签,您可以使用这种方式:
<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}%">
in this way you can visualize the value of progressbar with a % added.
通过这种方式,您可以通过添加 % 来可视化进度条的值。
回答by H.B.
You can create a converter that takes the input string and adds the ":".
您可以创建一个转换器,它接受输入字符串并添加“:”。
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:
Xml:
<Window.Resources>
<local:AddStringToStringConverter x:Key="AddStringToStringConverter"/>
</Window.Resources>
...
<Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/>
Or something like that. Tried it and it worked for my source at least.
或类似的东西。试过了,它至少对我的来源有用。
If you have whitespace and the like in you ConverterParameter
you can use signle quotes to make sure it does not get disposed.
如果您有空格等,您ConverterParameter
可以使用单引号来确保它不会被处理。
Edit:Oh right... yeah... there's also StringFormat
which i have never needed before, ehehehe...
编辑:哦,对了......是的......还有StringFormat
我以前从未需要过的,呵呵......