简体   繁体   中英

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.

I have an ObservableCollection of ints in the ViewModel property MySimpleData. Associated with each int is a canvas. 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. How can I retrieve my canvas for each index?

I am looking for soemthing along the lines of:

MyListView.Items.(Related-DataTemplate).(Related-Canvas)

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(); and then for the picture-objects for that canvas I will call related-canvas.addChild(Relevant-Picture-Object);

Here is my xaml in case it helps.

<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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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