How can I extract the background color of a tapped canvas?

I want to allow the user to select the color they want to use for the AppBar. I've got XAML like so:

<StackPanel Orientation="Horizontal">
    <Canvas Background="Aqua" Width="20" Height="20" VerticalAlignment="Center" Tapped="CanvasColor_Tapped"></Canvas>
    <TextBlock Text="Aqua" VerticalAlignment="Center"></TextBlock>
</StackPanel>

...and this idea for the handler:

private void CanvasColor_Tapped(object sender, TappedRoutedEventArgs treArgs)
{
    if (sender is Canvas)
    {
        Color colour = (Canvas) sender.Background;
    }

}

...but the compiler and my cranium are not cooperating / not on the same wavelength. Specifically, I get:

'object' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

What need I to do to share an event handler among all the Canvas controls to extract the background color of the tapped canvas?

Upvotes: 0

Views: 74

Answers (1)

Piyush Parashar
Piyush Parashar

Reputation: 874

I guess you are missing a pair of parenthesis.

Color colour = ((Canvas)sender).Background;

The compiler is looking for Background property in sender object which is of type object.

Upvotes: 1

Related Questions