Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Changing the color of a line element in Xaml using ColorPicker on C# for WinUI3

jv1597

Coder
I'm trying to bind a button click command to a function in C# for WinUI3 to change the color of a line element.
I get the following error message:
CS0029: Cannot implicitely convert 'Windows.UI.Color' to 'System Drawing Color'

I copied the code from Microsoft's Learn website. The first issue I had was with the cp.Color property.


Here's what I have so far:
Code:
Xaml code:
<Grid Width="110" Height="68" Canvas.Top="200" Canvas.Left="200" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ColorPicker ColorChanged="cpe" IsAlphaEnabled="False" IsColorPreviewVisible="True" IsColorSliderVisible="True" IsHexInputVisible="True" IsMoreButtonVisible="True"></ColorPicker>
    <Button x:Name="confirm" Content="Confirm" Click="confirmColor_Click"></Button>
    <Button x:Name="cancel" Content="Cancel" Click="cancelColor_Click"></Button>
</Grid>

C# code:
private void confirmColor_Click(object sender, RoutedEventArgs e)
{
    ColorPicker cp = new ColorPicker();
    Color clr = cp.Color;
    btn.Flyout.Hide();
    ln1.Stroke = clr;
}

I've tried the following:
Code:
private void confirmColor_Click(object sender, RoutedEventArgs e)
{
    ColorPicker cp = new ColorPicker();
    Color clr = cp.Color;
    Brush brsh = clr;
    btn.Flyout.Hide();
    ln1.Stroke = brsh;
}
 
It appears that Color clr defaults to System.Drawing.Color. Replace the line with
Code:
Windows.UI.Color clr = cp.Color;
 

New Threads

Buy us a coffee!

Back
Top Bottom