Tar
Tar

Reputation: 9045

How to place canvas with drawings inside certain column of a ListView control?

How to place canvas with drawings inside certain column of a ListView control ?

I have this:

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

and:

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

but it shows System.Windows.Controls.Canvas as a text, and not the canvas itself with its drawings.

Upvotes: 2

Views: 688

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

It's working, check example below.

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:

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

Code-behind:

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;

Result:

enter image description here

Upvotes: 2

Related Questions