我使用了this page中所示的DataTemplateSelector示例,并对其进行了一些修改,以满足我的要求,如下所示。
public abstract class TemplateSelector : ContentControl
{
public abstract DataTemplate SelectTemplate(object item, DependencyObject container);
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
var parent = GetParentByType<LongListSelector>(this);
ContentTemplate = SelectTemplate(newContent, this);
}
private static T GetParentByType<T>(DependencyObject element) where T : FrameworkElement
{
T result = null;
DependencyObject parent = VisualTreeHelper.GetParent(element);
while (parent != null)
{
result = parent as T;
if (result != null)
{
return result;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
}
public class MyTemplateSelector : TemplateSelector
{
public DataTemplate one { get; set; }
public DataTemplate two { get; set; }
public DataTemplate three { get; set; }
public DataTemplate four { get; set; }
public DataTemplate five { get; set; }
public DataTemplate six { get; set; }
public DataTemplate seven { get; set; }
public DataTemplate eight { get; set; }
public DataTemplate nine { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
PoiData poiItem = item as PoiData;
if (poiItem != null)
{
switch (poiItem.Type)
{
case "atm":
return one;
case "food":
return two;
case "hospital":
return three;
case "police":
return four;
case "pharmacy":
return five;
case "gas_station":
return six;
case "hindu_temple":
return seven;
case "train_station":
return eight;
case "movie_theater":
return nine;
}
}
return null;
}
}
我的xaml看起来像这样。
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="one">
<StackPanel Height="75">
<Grid Height="65"
Background="Red"
HorizontalAlignment="Left"
Margin="0, 0, 0, 12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="65"
Height="65"
Background="White"
Grid.Column="0"
Margin="0, 0, 0, 0">
</Grid>
<Grid Grid.Column="1" Width="auto">
<TextBlock Text="{Binding Title}"
FontSize="30" Margin="10,0,0,0"
VerticalAlignment="Center"
Foreground="White"/>
</Grid>
</Grid>
</StackPanel>
</DataTemplate>
.
.
.
</phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="SelectingTemplate">
<local:MyTemplateSelector Content="{Binding}"
one="{StaticResource one}"
two="{StaticResource two}"
three="{StaticResource three}"
four="{StaticResource four}"
five="{StaticResource five}"
six="{StaticResource six}"
seven="{StaticResource seven}"
eight="{StaticResource eight}"
nine="{StaticResource nine}" />
</DataTemplate>
这是全景项目xaml
<phone:PanoramaItem Header="{Binding Path=LocalizedResources.AroundMe, Source={StaticResource LocalizedStrings}}">
<!--Double wide Panorama with large image placeholders-->
<phone:LongListSelector Margin="12,-20,0,75"
ItemsSource="{Binding Poi.Items}"
ItemTemplate="{StaticResource SelectingTemplate}"
SelectionChanged="PoiSelectionChanged" >
</phone:LongListSelector>
</phone:PanoramaItem>
我的问题是,如果我使用此代码,stackpanel
始终将自己与panorama
项的中心对齐,无论我将HorizontalAlignment
设置为left
还是right
或stretch
。
如果我将width
或子stackpanel
的{{1}}属性设置为grid
以占用整个屏幕空间,我会收到错误认为是完全无关紧要的。
*
我应该做些什么来设置{MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---}
的宽度以占据整个屏幕宽度?有没有解决方法?
当我将stackpanel
设置为width
时,为什么会抛出此异常。
答案 0 :(得分:2)
解决问题的两件事
为什么这样做?
答案 1 :(得分:1)
我找到了解决方案!我在这里找到了谷歌搜索:http://webcache.googleusercontent.com/search?q=cache:KDZZtrw9eG0J:y2bd.me/blog/2013/08/16/fixing-alignment-issues-with-datatemplateselector/+&cd=6&hl=en&ct=clnk&gl=us
基本上,引用TemplateSelector的XAML标记需要将HorizontalContentAlignment设置为stretch。像这样:
<DataTemplate x:Key="YourTemplateSelectorKey">
<namespace:YourTemplateSelectorClass HorizontalContentAlignment="Stretch" ... />
</DataTemplate>
多田!