Concat content of ContentPresenter
本文关键字:ContentPresenter of content Concat | 更新日期: 2023-09-27 18:28:24
尝试在progressBar中设置文本,我可以正确地使用Setters。但我必须将这个值与连接起来
{TemplateBinding Value}+"% Completed
我如何连接其他文本。
在进度条内打印文本的代码:
<Border x:Name="whiteBorder" >
<ContentPresenter x:Name="perHolder" Content="{TemplateBinding Value}" VerticalAlignment="Center" HorizontalAlignment="Right"/>
</Border>
Silverlight 3.0版
另一个选项是使用内联元素,如runs:
<TextBlock>
<Run Text="{TemplateBinding Value}"/>
<Run Text="% Completed "/>
</TextBlock>
编辑:
看过您的示例后,我认为您不应该在模板中使用内容演示器。内容呈现器用于可以承载内容的控件(Read:具有内容属性)。当给定一个字符串作为内容时,内容呈现器肯定会显示该字符串,您可以使用它,但对于字符串,您最好选择文本块。它还使oyu能够更好地控制字符串的显示方式。
还有,我的坏。TemplateBinding对它的可用位置很挑剔,内联元素不在他的列表中。你最好的选择就像克里斯·W说的那样,使用堆叠面板:
<Border x:Name="whiteBorder" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right">
<TextBlock Text="{TemplateBinding Value}"/>
<TextBlock Text="% Completed"/>
</StackPanel>/>
</Border>
或者。。。你也可以只使用StringFormat
<ContentPresenter x:Name="perHolder"
Content="{TemplateBinding Value, StringFormat=''{'0}% Completed'}"
VerticalAlignment="Center" HorizontalAlignment="Right"/>
希望这能帮助。。。
要增加值,请使用一个实现IValueConverter
的类,如下所述:
http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx
本质上,实现IValueConverter
的类将在名为Convert
的方法中中介value
。从该方法返回的值是您真正想要显示的值。考虑:
命名空间Sharp{
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}
中。