1

在列表视图的 WinJS 应用程序itemTemplate属性中,可以接受一个函数,我可以在其中手动创建元素。

这种方法在 XAML/C# 应用程序中的模拟是什么?

我知道 DataTemplate 选择器,但我需要手动创建项目,因为性能原因我想摆脱模板。

4

3 回答 3

0

I think you can make a CustomControl inheriting from ItemsControl.

   public class BuildingComparer : ItemsControl
      {} 

There, you will find some methods to override:

 protected override DependencyObject GetContainerForItemOverride()
    {
        var container = new ContentPresenter();           
        //Do Stuff
        return container;            
    }

And you have access to the Items property so you can paint the elements whenever the SizeChanged event occurs, you can call to a Method wich manually paints all the elements.

Hope it helps you.

于 2013-10-03T08:08:05.240 回答
0

要在 C#/XAML 中完成此操作,您需要将 ItemsControl 的ItemsSource属性绑定到为您创建项目的属性。

示例 XAML:

<ItemsControl ItemsSource="{Binding SourceProperty}" />

示例数据上下文:

public IEnumerable SourceProperty
{
    get
    {
        yield return new TextBlock(new Run("First"));
        yield return new TextBlock(new Run("Second"));
        yield return new TextBlock(new Run("Third"));
    }
}

编辑:如果您绝对必须避免所有数据绑定(我不确定您为什么要这样做),您可以ItemsSource在代码隐藏中分配:

更新的 XAML:

<ItemsControl Name="MyItemsControl" />

后面的代码:

public MainWindow()
{
    InitializeComponent();

    MyItemsControl.ItemsSource = SourceProperty;
}
于 2013-10-09T14:18:42.260 回答
0

你见过 DataTemplateSelector 吗?http://code.msdn.microsoft.com/windowsapps/The-DataTemplateSelector-93e46ad7

于 2013-09-30T04:11:46.037 回答