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

Kai 81 Reputation points
2019-12-17T12:08:18.303+00:00

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?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,196 Reputation points
    2019-12-18T02:14:09.96+00:00

    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);
        });
    }
    

0 additional answers

Sort by: Most helpful