Cedric Royer-Bertrand
Cedric Royer-Bertrand

Reputation: 741

How to redraw canvas after OnSelectedChanged in Avalonia?

In Avalonia 11 with MVVM toolkit, I have a containing a and . I would like to update the when the SelectedItem on the grid change.The canvas should display specific drawing depending on the item selected, so I need to use c# (maybe in the code behind?) to draw everything.

So far I have been able to:

How to link the event in a place where I have access to the ?

Upvotes: 0

Views: 444

Answers (1)

Cedric Royer-Bertrand
Cedric Royer-Bertrand

Reputation: 741

The best solution I found so far is to create ObservableCollections for each of the shape I want to draw on my canvas and make a binding in the view. That is also more MVVM approach.

Here is an example for future readers that have the same problem:

<Canvas Width="{Binding Width}" Height="{Binding Height}" MaxWidth="200" MaxHeight="100" Background="Yellow" Margin="3">
    <ItemsControl ItemsSource="{Binding Tiles}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Fill="Green" Height="{Binding Size}" Width="{Binding Size}" Canvas.Left="20" Canvas.Top="{Binding TopY}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Canvas>

Upvotes: 0

Related Questions