我想创建一个像 photoshop 主画布这样的应用程序,它将图层画布分组,
一个列表视图,它通过添加到主画布的每个层的数据模板显示层的名称和可见性按钮。
我创建了从类 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);
}
}
列表视图层:
<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>
如果我添加简单的画布对象只是为了测试它是否有效
and with DataTemplate DataType="Layer" and ObservableCollection<Layer>
如果我添加层对象执行挂起
我不确定为什么你的应用程序挂起,但我建议不要提到“图层是画布”,而是“图层有画布”。看到这个post.
所以,它会是这样的:
public class Layer
{
public string Name { get; set; } = string.Empty;
public Canvas Canvas { get; }
public Layer(Canvas canvas)
{
Canvas = canvas;
}
}
并像这样添加图层:
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);
}