ItemTemplateSelector 不适用于没有绑定的 ListView
ItemTemplateSelector not working for ListView without Binding
我有一个 ListView,我在后面的代码中以编程方式设置了它的 ItemsSource。我以前只有一个模板,但现在我想使用多个模板,select 根据我添加到 ListView 的项目使用模板。
这是我的 XAML 代码:
<Page.Resources>
<local:TemplateSelector x:Key="myTemplateSelector" TemplateA="{StaticResource TemplateA}" TemplateB="{StaticResource TemplateB}" />
<!-- TemplateA and TemplateB -->
</Page.Resources>
<ListView
x:Name="MasterListView"
Grid.Row="1"
ItemContainerTransitions="{x:Null}"
ItemTemplateSelector="{StaticResource myTemplateSelector}"
IsItemClickEnabled="True"
ItemClick="MasterListView_ItemClick"/>
然后我设置 ListView 的 ItemSource,这是我的 TemplateSelector:
public class TemplateSelector : DataTemplateSelector
{
public DataTemplate TemplateA { get; set; }
public DataTemplate TemplateB { get; set; }
public new DataTemplate SelectTemplate(object item, DependencyObject container)
{
return TemplateA;
}
}
但是,这不起作用。而不是我想要的模板,我总是为每个项目获得相同的文本:AppName.ViewModels.ViewModel1(我想显示的数据类型)。
过去,当我只有一个项目模板时,我在 xaml 中使用了它并且它有效:
<ListView
x:Name="MasterListView"
Grid.Row="1"
ItemContainerTransitions="{x:Null}"
ItemTemplate="{StaticResource TemplateA}"
IsItemClickEnabled="True"
ItemClick="MasterListView_ItemClick"/>
如何让 ItemTemplateSelector 工作?我在那里放置了断点,但它甚至没有被调用。
public override DataTemplate SelectTemplate(object item, DependencyObject container)
而不是
public new DataTemplate SelectTemplate(object item, DependencyObject container)
编辑,由于访问修饰符,这是 4.5.2 的签名(注意它是 public):
#region Assembly PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\PresentationFramework.dll
#endregion
namespace System.Windows.Controls
{
//
// Summary:
// Provides a way to choose a System.Windows.DataTemplate based on the data object
// and the data-bound element.
public class DataTemplateSelector
{
//
// Summary:
// Initializes a new instance of the System.Windows.Controls.DataTemplateSelector
// class.
public DataTemplateSelector();
//
// Summary:
// When overridden in a derived class, returns a System.Windows.DataTemplate based
// on custom logic.
//
// Parameters:
// item:
// The data object for which to select the template.
//
// container:
// The data-bound object.
//
// Returns:
// Returns a System.Windows.DataTemplate or null. The default value is null.
public virtual DataTemplate SelectTemplate(object item, DependencyObject container);
}
}
如 DataTemplateSelector.SelectTemplate(Object, DependencyObject) method 的 备注 中所述:
App code typically doesn't call SelectTemplate methods; the methods exists so that the infrastructure can call it while choosing the correct templates based on using a DataTemplateSelector instance from a property value such as ItemsControl.ItemsTemplateSelector. To provide a specific template in a derived class, override the SelectTemplateCore(Object, DependencyObject) method.
所以我们应该使用
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
而不是
public new DataTemplate SelectTemplate(object item, DependencyObject container)
我有一个 ListView,我在后面的代码中以编程方式设置了它的 ItemsSource。我以前只有一个模板,但现在我想使用多个模板,select 根据我添加到 ListView 的项目使用模板。
这是我的 XAML 代码:
<Page.Resources>
<local:TemplateSelector x:Key="myTemplateSelector" TemplateA="{StaticResource TemplateA}" TemplateB="{StaticResource TemplateB}" />
<!-- TemplateA and TemplateB -->
</Page.Resources>
<ListView
x:Name="MasterListView"
Grid.Row="1"
ItemContainerTransitions="{x:Null}"
ItemTemplateSelector="{StaticResource myTemplateSelector}"
IsItemClickEnabled="True"
ItemClick="MasterListView_ItemClick"/>
然后我设置 ListView 的 ItemSource,这是我的 TemplateSelector:
public class TemplateSelector : DataTemplateSelector
{
public DataTemplate TemplateA { get; set; }
public DataTemplate TemplateB { get; set; }
public new DataTemplate SelectTemplate(object item, DependencyObject container)
{
return TemplateA;
}
}
但是,这不起作用。而不是我想要的模板,我总是为每个项目获得相同的文本:AppName.ViewModels.ViewModel1(我想显示的数据类型)。
过去,当我只有一个项目模板时,我在 xaml 中使用了它并且它有效:
<ListView
x:Name="MasterListView"
Grid.Row="1"
ItemContainerTransitions="{x:Null}"
ItemTemplate="{StaticResource TemplateA}"
IsItemClickEnabled="True"
ItemClick="MasterListView_ItemClick"/>
如何让 ItemTemplateSelector 工作?我在那里放置了断点,但它甚至没有被调用。
public override DataTemplate SelectTemplate(object item, DependencyObject container)
而不是
public new DataTemplate SelectTemplate(object item, DependencyObject container)
编辑,由于访问修饰符,这是 4.5.2 的签名(注意它是 public):
#region Assembly PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\PresentationFramework.dll
#endregion
namespace System.Windows.Controls
{
//
// Summary:
// Provides a way to choose a System.Windows.DataTemplate based on the data object
// and the data-bound element.
public class DataTemplateSelector
{
//
// Summary:
// Initializes a new instance of the System.Windows.Controls.DataTemplateSelector
// class.
public DataTemplateSelector();
//
// Summary:
// When overridden in a derived class, returns a System.Windows.DataTemplate based
// on custom logic.
//
// Parameters:
// item:
// The data object for which to select the template.
//
// container:
// The data-bound object.
//
// Returns:
// Returns a System.Windows.DataTemplate or null. The default value is null.
public virtual DataTemplate SelectTemplate(object item, DependencyObject container);
}
}
如 DataTemplateSelector.SelectTemplate(Object, DependencyObject) method 的 备注 中所述:
App code typically doesn't call SelectTemplate methods; the methods exists so that the infrastructure can call it while choosing the correct templates based on using a DataTemplateSelector instance from a property value such as ItemsControl.ItemsTemplateSelector. To provide a specific template in a derived class, override the SelectTemplateCore(Object, DependencyObject) method.
所以我们应该使用
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
而不是
public new DataTemplate SelectTemplate(object item, DependencyObject container)