LongListSelector 更改 ItemTemplate [英] LongListSelector change ItemTemplate
问题描述
我想动态更改 LongListSelector
的 ItemTemplate
.我通过将它绑定到我的 ViewModel 中的一个属性来做到这一点:
I want to dynamically change the ItemTemplate
of a LongListSelector
. I do this by Binding it to a property in my ViewModel:
<toolkit:LongListSelector ItemTemplate="{Binding ItemTemplate}" ItemsSource="{Binding Items}" />
到目前为止,这有效.问题是,如果我在 ViewModel 中更改模板的属性,则 LongListSelector
不会应用更改后的模板.
This works so far. The problem is that if I change the property for the template in my ViewModel then the LongListSelector
doesn't apply the changed template.
我查看了调试器,每当我使用它的 setter 更改属性时,getter 都会依次调用,因此 LongListSelector
读取新模板(但不应用它).
I looked in the debugger, whenever I change the property by using it's setter, then the getter gets called in turn, so the LongListSelector
reads the new template (but doesn't apply it).
这是一个错误,还是无法通过绑定更改ItemTemplate
?
Is this a bug, or is it not possible to change the ItemTemplate
via a binding?
推荐答案
LongListSelector 忽略对其 ItemTemplate 的更改.LongListSelector 中的 DependencyProperty 定义如下所示:
The LongListSelector ignores changes to its ItemTemplate. The DependencyProperty definition in LongListSelector looks like this:
public static readonly DependencyProperty ItemsTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate),
typeof(LongListSelector),
new PropertyMetadata(null));
如果你想让它注意到变化,你可以像这样获取源代码并重建它:
If you want it to notice changes, you can fetch the source and rebuild it like this:
public static readonly DependencyProperty ItemsTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate),
typeof(LongListSelector),
new PropertyMetadata(null,
OnItemsTemplateChanged));
private static void OnItemsTemplateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
((LongListSelector)obj).OnItemsTemplateChanged();
}
private void OnItemsTemplateChanged()
{
_flattenedItems = null;
if (_isLoaded)
{
EnsureData();
}
}
这篇关于LongListSelector 更改 ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!