WinUI 3 ListView Linked To Observablecollection contains layer classes derived from class canvas

mustapha chaibi 65 Reputation points
2023-02-23T15:05:49.5733333+00:00

I want to create an application like photoshop main canvas which groups layer canvases,

a listview which displays the name of the layers and a visibility button via a datatemplate of each layer added to the main canvas.

I created the class layer derived from the class convas

public class Layer : Canvas
{
     public string Name { get; set; }
     public bool IsVisible { get; set; }
    
     public Layer(Canvas mainCanvas)
     {
         // Initialize layer properties
         Name = "New Layer";
         IsVisible = true;
        mainCanvas.Children.Add(this);
     }
}

ListView Layers

<StackPanel
     Grid.Row="1"
     Orientation="Vertical">
     <ListView
         x:Name="listViewLayers"
         x:FieldEdit="public"
         Background="SeaGreen">
         <ListView.ItemTemplate>
             <DataTemplate x:DataType="Layer">
                 <StackPanel>
                     <TextBlock Text="{x:Bind Name, Mode=TowWay}" />
                 </StackPanel>
             </DataTemplate>
         </ListView.ItemTemplate>
     </ListView>
</StackPanel>

ObservableCollection ocLayers :

ObservableCollection<Layer> ocLayers = new ObservableCollection<Layer>();

                ocLayers.Add(new Layer(mainCanvas){Name="AAA"});
         
                ocLayers.Add(new Layer(mainCanvas){Name="BBB"});
      
 listViewLayers.ItemsSource = ocLayers;

DataTemplate with DataType="Canvas" and ObservableCollection<Canvas>

if i add simple canvas Object just for testing it works

DataTemplate with DataType="Layer" and ObservableCollection<Layer>

if i add Layer Object execution hangs

Windows development | Windows App SDK
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2023-02-24T02:52:27.44+00:00

    The two Layers which used by listViewLayers ListView cannot be added by another Canvas anymore. As far as I'm concerned, the ListView should just contain string items which control Canvas behavior instead of Canvas in ListView.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.