WinUI 3 ListView 链接到包含继承自 Canvas 类的层类的 ObservableCollection。

huangapple go评论120阅读模式
英文:

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

问题

我想创建一个类似于Photoshop主画布的应用程序,其中包括分组的图层画布、一个显示图层名称和可见性按钮的ListView,以及每个图层的数据模板添加到主画布。

我创建了一个名为Layer的类,它派生自Canvas类:

public class Layer : Canvas
{
    public string Name { get; set; }
    public bool IsVisible { get; set; }

    public Layer(Canvas mainCanvas)
    {
        // 初始化图层属性
        Name = "New Layer";
        IsVisible = true;
        mainCanvas.Children.Add(this);
    }
}

图层的ListView:

<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=TwoWay}" />
                </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;

如果我添加一个简单的Canvas对象进行测试,它可以正常工作。

但是,如果我使用DataTemplate的DataType为"Layer"和ObservableCollection,当我添加Layer对象时,执行会卡住。

请检查Layer类的实现以确保它没有导致执行停滞。您可能需要进一步调查和排除问题。

英文:

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 = &quot;New Layer&quot;;
      IsVisible = true; 
      mainCanvas.Children.Add(this);  
  }
}

ListView Layers :

&lt;StackPanelGrid.Row=&quot;1&quot;Orientation=&quot;Vertical&quot;&gt; 
    &lt;ListViewx:Name=&quot;listViewLayers&quot;x:FieldEdit=&quot;public&quot;Background=&quot;SeaGreen&quot;&gt;
       &lt;ListView.ItemTemplate&gt;
          &lt;DataTemplate x:DataType=&quot;Layer&quot;&gt;
            &lt;StackPanel&gt;
               &lt;TextBlock Text=&quot;{x:Bind Name, Mode=TowWay}&quot; /&gt;
            &lt;/StackPanel&gt;
       &lt;/DataTemplate&gt;
     &lt;/ListView.ItemTemplate&gt;
    &lt;/ListView&gt;
&lt;/StackPanel&gt;

ObservableCollection ocLayers :

ObservableCollection&lt;Layer&gt; ocLayers = new ObservableCollection&lt;Layer&gt;();  
             ocLayers.Add(new Layer(mainCanvas){Name=&quot;AAA&quot;});
             ocLayers.Add(new Layer(mainCanvas){Name=&quot;BBB&quot;});
             listViewLayers.ItemsSource = ocLayers;
with DataTemplate DataType=&quot;Canvas&quot; and ObservableCollection&lt;Canvas&gt;

if i add simple canvas Object just for testing it works

and with DataTemplate DataType=&quot;Layer&quot; and ObservableCollection&lt;Layer&gt;

if i add Layer Object execution hangs

答案1

得分: 0

I'm not sure why your app is hangs but let mentioned that instead of "Layer is a Canvas", I suggest "Layer has a Canvas". See this post.

So, it'd be something like this:

public class Layer
{
    public string Name { get; set; } = string.Empty;

    public Canvas Canvas { get; }

    public Layer(Canvas canvas)
    {
        Canvas = canvas;
    }
}

And add layers like this:

private void AddLayerButton_Click(object sender, RoutedEventArgs e)
{
    int layerId = Layers.Count + 1;
    Layer newLayer = new(new Canvas())
    {
        Name = $"New Layer #{layerId}"
    };
    this.mainCanvas.Children.Add(newLayer.Canvas);
    Layers.Add(newLayer);
}
英文:

I'm not sure why your app is hangs but let mentioned that instead of "Layer is a Canvas", I suggest "Layer has a Canvas". See this post.

So, it'd be something like this:

public class Layer
{
    public string Name { get; set; } = string.Empty;

    public Canvas Canvas { get; }

    public Layer(Canvas canvas)
    {
        Canvas = canvas;
    }
}

And add layers like this:

private void AddLayerButton_Click(object sender, RoutedEventArgs e)
{
    int layerId = Layers.Count + 1;
    Layer newLayer = new(new Canvas())
    {
        Name = $&quot;New Layer #{layerId}&quot;
    };
    this.mainCanvas.Children.Add(newLayer.Canvas);
    Layers.Add(newLayer);
}

huangapple
  • 本文由 发表于 2023年2月24日 04:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定