尝试在progressBar中设置文本,我可以正确地使用Setter。但是我必须将这个值与:
{TemplateBinding Value}+"% Completed 如何与其他文本连接。
在进度栏中打印文本的代码:
<Border x:Name="whiteBorder" >
<ContentPresenter x:Name="perHolder" Content="{TemplateBinding Value}" VerticalAlignment="Center" HorizontalAlignment="Right"/>
</Border>Silverlight 3.0版
发布于 2013-08-22 00:04:05
另一种选择是使用inline elements,比如runs:
<TextBlock>
<Run Text="{TemplateBinding Value}"/>
<Run Text="% Completed "/>
</TextBlock>编辑:
看过你的例子后,我认为你不应该在你的模板中使用content presenter。Content presenter用于可以承载内容的控件(Read:具有content属性)。当content presenter提供一个字符串作为内容时,它肯定会显示该字符串,您可以使用它,但对于字符串,更好的选择是textblock。它还可以让oyu更好地控制字符串的显示方式。
还有,我的错。TemplateBinding对它在哪里可用很挑剔,内联元素不在他的列表中。你最好的选择就像Cris W.说的那样,使用stackpanel:
<Border x:Name="whiteBorder" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right">
<TextBlock Text="{TemplateBinding Value}"/>
<TextBlock Text="% Completed"/>
</StackPanel>/>
</Border>发布于 2013-08-21 00:49:35
或者..。您也可以只使用StringFormat
<ContentPresenter x:Name="perHolder"
Content="{TemplateBinding Value, StringFormat='\{\0}% Completed'}"
VerticalAlignment="Center" HorizontalAlignment="Right"/>希望这能帮到你。
发布于 2013-08-20 22:37:52
要增加值,请使用实现IValueConverter的类,如下所述:
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
本质上,实现IValueConverter的类将在名为Convert的方法中与value进行交互。此方法返回的值是您真正想要显示的值。考虑一下:
命名空间锐化{
public class MyConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
// First cast the value to an 'int'.
Int32 theInputtedValue = (Int32)value;
// Then return the newly formatted string.
return String.Format("{0}% Completed", theInputtedValue);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}}
请注意,在上面的示例中,值转换器位于名称空间Sharp中。现在我们将其添加到XAML定义中:
<Window x:Class="Sharp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lol="clr-namespace:Sharp">最后一行是值转换器所在的命名空间(也就是说,lol现在将指向Sharp CLR命名空间。
接下来,我们将类添加到Window资源中:
<Window.Resources>
<lol:MyConverter x:Key="TheValueConverter" />
</Window.Resources>这将创建可在XAML中使用的类的实例。到目前为止,我们的XAML看起来是这样的:
<Window x:Class="Sharp.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lol="clr-namespace:Sharp">
<Window.Resources>
<lol:MyConverter x:Key="TheValueConverter" />
</Window.Resources>现在我们只需将其添加到您的内容呈现器中,如下所示:
<ContentPresenter Content="{TemplateBinding Value, Converter={StaticResource TheValueConverter}}" ... />这告诉ContentPresenter,当它表示值时,它应该使用TheValueConverter实例,这是我们的ValueConverter的一个实例。需要注意的两件事:(1)确保使用实例的名称(TheValueConverter)而不是类定义(MyConverter)。(2)确保将实例名称包装在{StaticResource}中。
https://stackoverflow.com/questions/18337750
复制相似问题