2

如何在控件的特定列内放置带有绘图的画布ListView

我有这个:

<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为文本,而不是画布本身及其绘图。

4

1 回答 1

2

它正在工作,请查看下面的示例。

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>

物品等级:

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;

结果:

在此处输入图像描述

于 2013-07-21T08:43:16.853 回答