简体   繁体   English

XAML / C#中来自WinJS模拟的模板渲染器

[英]Template renderer from WinJS analog in XAML/C#

In WinJS application itemTemplate property of listview can accept a function in which i can create elements manually. 在WinJS应用程序中,listview的itemTemplate属性可以接受一个函数,在其中我可以手动创建元素。

What is the analog for this approach in XAML/C# applications? XAML / C#应用程序中这种方法的模拟方式是什么?

I know about DataTemplate selectors, but I need to create items manually, I want rid off templates because of performance. 我知道DataTemplate选择器,但是我需要手动创建项目,由于性能原因我想摆脱模板。

I think you can make a CustomControl inheriting from ItemsControl. 我认为您可以使CustomControl继承自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. 而且您可以访问Items属性,因此只要发生SizeChanged事件,就可以绘制元素,可以调用方法来手动绘制所有元素。

Hope it helps you. 希望对您有帮助。

To accomplish this in C# / XAML, you need to bind the ItemsControl's ItemsSource property to a property that creates the items for you. 要在C#/ XAML中完成此操作,您需要将ItemsControl的ItemsSource属性绑定到为您创建项目的属性。

Example XAML: XAML示例:

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

Example DataContext: 示例DataContext:

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"));
    }
}

Edit: If you absolutely must avoid all databinding (I'm not sure why you would), you can assign the ItemsSource in the codebehind: 编辑:如果绝对必须避免所有数据绑定(我不确定为什么会这样),则可以在ItemsSource中分配ItemsSource

Updated XAML: 更新的XAML:

<ItemsControl Name="MyItemsControl" />

Code behind: 后面的代码:

public MainWindow()
{
    InitializeComponent();

    MyItemsControl.ItemsSource = SourceProperty;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM