question

Kai-4867 avatar image
0 Votes"
Kai-4867 asked ·

[cpp][uwp] c++ winrt How to get app width and bind it to a Slider

in I am trying to bind the upper end of a Slider to the current width of the app. It needs to look like,

< Slider x:Name="Slider1" Width="200" Minimum="50" Maximum="Page-Width" StepFrequency="10"

How can I get the page width in c++ winrt? How can I listen to a change event for when the window is resized and update the slider?

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

Hello,

​Welcome to Microsoft Q&A!

How to get app width

You can get the current width of window by using Window::Current().Bounds().Width method.

How can I listen to a change event for when the window is resized and update the slider?

There is a CoreWindow.SizeChanged Event can be used to listen for window size changes. You can directly change the Maximum in code-behind or create a ViewModel to bind to the Maximum of Slider and change the property of ViewModel to change the Maximum.

 #include "winrt/Windows.UI.Core.h"
    
 MainPage::MainPage()
 {
     InitializeComponent();
     Window::Current().CoreWindow().SizeChanged([this](Windows::UI::Core::CoreWindow const&, Windows::UI::Core::WindowSizeChangedEventArgs const&) {
         Slider1().Maximum(Windows::UI::Xaml::Window::Current().Bounds().Width);
     });
 }


· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I needed to cast Windows::UI::Xaml::Window::Current().Bounds().Width to an int but otherwise it worked great. Thank you.

0 Votes 0 ·