英文:
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类的实现以确保它没有导致执行停滞。您可能需要进一步调查和排除问题。
英文:
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 :
<StackPanelGrid.Row="1"Orientation="Vertical">
<ListViewx: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;
with DataTemplate DataType="Canvas" and ObservableCollection<Canvas>
if i add simple canvas Object just for testing it works
and with DataTemplate DataType="Layer" and ObservableCollection<Layer>
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 = $"New Layer #{layerId}"
};
this.mainCanvas.Children.Add(newLayer.Canvas);
Layers.Add(newLayer);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论