简体   繁体   English

从代码检索从列表视图的数据模板动态生成的对象

[英]From Code Retrieve dynamically generated objects from listview's datatemplate

The key here in the title is that I want to retrieve an object, specifically a canvas, that is contained in my listviewitem's datatemplate. 标题中的关键是我想检索一个包含在listviewitem的数据模板中的对象,尤其是画布。

I have an ObservableCollection of ints in the ViewModel property MySimpleData. 我在ViewModel属性MySimpleData中有一个ObtsableCollection int Associated with each int is a canvas. 与每个int相关联的是一个画布。 Essentially I am using a listview to display an array of "pictures". 本质上,我使用列表视图显示“图片”数组。 The user can click buttons which changes the contents of the "Canvases". 用户可以单击按钮来更改“画布”的内容。 However a single picture object can last many frames so I don't store them in the canvas but rather in a separate location with a start index and a duration. 但是,单个图片对象可以持续许多帧,因此我不将其存储在画布中,而是存储在具有起始索引和持续时间的单独位置。 I would like to procedurally generate each of the canvases in my listview. 我想以程序方式在listview中生成每个画布。 How can I retrieve my canvas for each index? 如何为每个索引检索画布?

I am looking for soemthing along the lines of: 我正在寻找以下方面的东西:

MyListView.Items.(Related-DataTemplate).(Related-Canvas) MyListView.Items。(相关数据模板)。(相关画布)

My goal is to essentially clear all the canvases and re-draw/refresh them when I want. 我的目标是从本质上清除所有画布,并在需要时重新绘制/刷新它们。 This is a mock up / demo so I don't mind if the solution is a bit hacky. 这是一个模拟/演示,所以我不介意解决方案是否有点怪异。 I just need something that works well enough and won't require me to write my own control. 我只需要一些效果很好的东西,而无需我编写自己的控件。

My intent would be to iterate MyListView.items essentially call, related-canvas.clear(); 我的意图是本质上迭代MyListView.items调用,related-canvas.clear();。 and then for the picture-objects for that canvas I will call related-canvas.addChild(Relevant-Picture-Object); 然后对于该画布的图片对象,我将调用related-canvas.addChild(Relevant-Picture-Object);

Here is my xaml in case it helps. 这是我的xaml,以防万一。

<ListView Name="MyListView" 
    ItemsSource="{Binding MySimpleData}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Column1"
                                DisplayMemberBinding="{Binding}"/>
                <GridViewColumn Header="Column2-Canvases" 
                    Width="{Binding DataContext.CanvasWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Canvas
                                Width="{Binding DataContext.CanvasWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                                Height="{Binding DataContext.CanvasHeight, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                Background="LightSlateGray"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

Any ideas would are greatly appreciated. 任何想法将不胜感激。

Cheers. 干杯。

So I ended up replacing the collection with a collection of canvas objects. 因此,我最终将其替换为画布对象的集合。 The next step was to replace the binding and use a content presenter, to directly display the canvases that were in my collection. 下一步是替换绑定并使用内容演示者直接显示我收藏中的画布。 One other useful change I made was to the column 1 code to display the index of my items. 我进行的另一项有用更改是对第1列代码进行了显示,以显示我的商品的索引。 Not as hacky as I was looking for, but arguably hacky enough. 不像我想要的那样笨拙,但是可以说足够笨拙。

<ListView Name="MyListView" 
<!-- COMMENT ItemsSource="{Binding MySimpleData}"> END COMMENT-->
ItemsSource="{Binding CanvasCollection}"> <!-- EDIT HERE -->
AlternationCount="{Binding CanvasCollection.Count}"
<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.View>
    <GridView>
        <GridView.Columns>
          <!-- COMMENT <GridViewColumn Header="Column1"
          DisplayMemberBinding="{Binding}"/> END COMMENT -->
              <GridViewColumn Header="Index" Width="37">
                <GridViewColumn.CellTemplate> <!-- NEW Index displaying code -->
                        <DataTemplate>
                            <Label Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=(ItemsControl.AlternationIndex)}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            <GridViewColumn Header="Column2-Canvases" 
                            Width="{Binding DataContext.CanvasWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
          <!-- COMMENT      <Canvas
                            Width="{Binding DataContext.CanvasWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                            Height="{Binding DataContext.CanvasHeight, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                            Background="LightSlateGray"/> END COMMENT -->
                            <ContentPresenter Content="{Binding}" /> <!-- NEW -->
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView.Columns>
    </GridView>
</ListView.View>

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

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