gpt4 book ai didi

c# - 如何在 ListView 控件的特定列中放置带有绘图的 Canvas ?

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:53 25 4
gpt4 key购买 nike

如何在 ListView 控件的特定列中放置带有绘图的 Canvas ?

我有这个:

<DataTemplate>
<Canvas Width="60" Height="20" Background="Red" ClipToBounds="True" >
<ContentPresenter Content="{Binding Path=Graph}" />
</Canvas>
</DataTemplate>

和:

var canvas = new Canvas();
canvas.Children.Add(new Ellipse(){});
var items = new ObservableCollection<LvItem>() { new LvItem(){ Graph = canvas} };
myListView.ItemsSource = items;

但它将 System.Windows.Controls.Canvas 显示为文本,而不是 Canvas 本身及其绘图。

最佳答案

它正在运行,请检查下面的示例。

XAML:

<ListView Name="myListView" >
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Grap">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Canvas Width="60" Height="50" Background="Red" ClipToBounds="True" >
<ContentPresenter Content="{Binding Path=Graph}" />
</Canvas>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

LvItem 类:

class LvItem
{
public Canvas Graph { get; set; }
}

代码隐藏:

var canvas = new Canvas();
canvas.Children.Add(new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Colors.Yellow) });
var canvas2 = new Canvas();
canvas2.Children.Add(new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Colors.Blue) });
var items = new ObservableCollection<LvItem>() { new LvItem() { Graph = canvas }, new LvItem() { Graph = canvas2 } };
myListView.ItemsSource = items;

结果:

enter image description here

关于c# - 如何在 ListView 控件的特定列中放置带有绘图的 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17770312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com