[英]Assign an Image for each Pivot Item Header
I am trying to assign an image for each of my pivot items's header instead of a Text . 我正在尝试为每个数据透视表项的标题分配一个图像,而不是为Text分配一个图像。
I tried several methods (one was given by this post http://social.msdn.microsoft.com/Forums/wpapps/en-US/e7b5fd17-3465-4a94-81af-5c056c992c11/add-image-to-pivot-title?forum=wpdevelop ) 我尝试了几种方法(本文提供了一种方法http://social.msdn.microsoft.com/Forums/wpapps/en-US/e7b5fd17-3465-4a94-81af-5c056c992c11/add-image-to-pivot-title ?forum = wpdevelop )
I managed to assign the same image for my pivot but not one image for each header. 我设法为数据透视表分配了相同的图像,但没有为每个页眉分配一个图像。
This is what I tried : 这是我尝试的:
<phone:Pivot.HeaderTemplate >
<DataTemplate>
<Image Source="21.jpg" Height="55" Width="55"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
This obviously gave me the same image for each headers , 这显然给了我每个标题相同的图像,
So i wanted to try something like this : 所以我想尝试这样的事情:
<phone:Pivot.HeaderTemplate >
<DataTemplate>
<Image Source="{Binding}" Height="55" Width="55"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
[...]
<phone:PivotItem ??? >
<// phone:PivotItem >
But then i don't know what to add my image path. 但后来我不知道该添加什么图像路径。
i used this method when i wanted to assign a text as a header and it worked : 当我想将文本分配为标题时,我使用了此方法,并且可以正常工作:
<phone:Pivot.HeaderTemplate >
<DataTemplate>
<TextBlock Text="{Binding }" FontSize="88" />
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:PivotItem Header = "Title1" />
How can i assign an image for each of my Header ? 如何为每个标题分配图像?
You should be able to simply provide the image source in the Header: 您应该能够在Header中简单地提供图像源:
<phone:PivotItem Header = "21.jpg" />
This sets the data context to use for the HeaderTemplate
for that particular item. 这将设置用于特定项目的HeaderTemplate
的数据上下文。
You need to use a Converter Class to solve this issue. 您需要使用Converter类来解决此问题。
namespace MyImageConvertor
{
public class MyValueConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
var uri = new Uri((string)(value), UriKind.RelativeOrAbsolute);
var img = new BitmapImage(uri);
return img;
}
catch
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var img = value as BitmapImage;
return img.UriSource.AbsoluteUri;
}
#endregion
}
}
Then use the this convertor in your xaml. 然后在您的xaml中使用此转换器。
<UserControl x:Class="ValueConverter.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:this="clr-namespace:MyImageConvertor">
<UserControl.Resources>
<this:MyValueConverter x:Key="ImageConverter"/>
</UserControl.Resources>
<phone:Pivot>
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrlProperty, Converter={StaticResource ImageConverter},Mode=TwoWay}"></Image>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
</phone:Pivot>
Make sure you have the full image path in the ImageUrlProperty value like ..\\Images\\logo.png . 确保在ImageUrlProperty值中具有完整的图像路径,例如.. \\ Images \\ logo.png 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.