Search code examples
c++windows-runtimewinui

How can I set a stack panel's border color programmatically in C++ in WinUI3?


I am working on a project using WinUI 3 in C++, and I want to change the border color of a XAML control(e.g. stackpanel) according to some condition. I have tried search it online, but most of answers are in c#, and some in C++ I have tried but got no luck.

For example: ("StackPanel" is defined in the xaml )

StackPanel().BorderBrush(SolidColorBrush(ColorHelper::FromArgb(255, 255, 255, 255)));

Then the error would come up:

no instance of overloaded function matches the argument list
argument types are: (winrt::Windows::UI::Xaml::Media::SolidColorBrush)
object type is: winrt::Microsoft::UI::Xaml::Controls::StackPanel

And another one I tried in .cpp file:

StackPanel().BorderBrushProperty(SolidColorBrush(Colors::Black()));

the error is:

too many arguments in function call.

Why are these errors happening?

Could anyone help me on this? Or any suggestions?
Sample code would be great!

PS : I am still very new to WinUI 3 especially in C++ (there are not so much study material for C++)

I would be grateful for any help.


Solution

  • From what I'm seeing the actual error (as best as i can replicate what you're doing) is

    Severity    Code    Description Project File    Line    Suppression State
    Error   C2664   'void Windows::UI::Xaml::Controls::StackPanel::BorderBrush::set(Windows::UI::Xaml::Media::Brush ^)': cannot convert argument 1 from 'Windows::UI::Xaml::Media::SolidColorBrush' to 'Windows::UI::Xaml::Media::Brush ^'  App1    c:\repos\App1\MainPage.xaml.cpp 29  
    

    If you create your brush like this auto brush = ref new SolidColorBrush(...);
    And pass it to the stackpanel like this stackpanel->BorderBrush = brush;
    Your error should go away.

    Obviously how you are getting your stack panel might be different from how i did, but the point is you should be able to just set it to the value as long as you have the value in the correct form a ref new object in this case, it would seem.