user34299
user34299

Reputation: 397

How to get the Content of a XAML Button?

How do I get the Content of a XAML Button?

Visual Studio Community 15.9.4, with the C++/WinRT extensions installed.

I can set the Button's Content from the click handler, but cannot get the current Content.

Button definition from MainPage.xaml:

<Button x:Name="myButton" Click="ClickHandler">Click Me</Button>

Click handler definition from MainPage.cpp:

void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
  myButton().Content(box_value(L"Clicked"));
}

I have also found that this code works to set the Content:

void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
  Button sendButton = winrt::unbox_value<Button>(sender);

  sendButton.Content(box_value(L"Clicked"));
}

Code I have tried to get the Content just does not compile.

Upvotes: 0

Views: 556

Answers (1)

user34299
user34299

Reputation: 397

In posting the original question, a thought was triggered, which I tried and which worked. The following was added to the MainPage.cpp click handler:

IInspectable sendButtonContent = sendButton.Content();
hstring sendButtonString = unbox_value<hstring>(sendButtonContent);

When the code is paused, after the Button is clicked, the value of sendButtonString is "Clicked".

Upvotes: 1

Related Questions