Binding a width Property RRS feed

  • Question

  • Hi All,

    I have a border control within a popup control for which I am currently setting the height control in code behind.

    PopupBorder.Width = Window.Current.CoreWindow.Bounds.Width;

    Is there an easy way to set the value of this property with binding?



    Wednesday, July 16, 2014 8:02 AM


  • Yes, there is an easy way to do this:

    1. Create a model class like this:

        public class Model
            public double BorderWidth
                get { return Window.Current.CoreWindow.Bounds.Width; } 

    2. Create an instance of the model and set it as the DataContext of your window. I go the simple way and do it like this:

            protected override void OnNavigatedTo(NavigationEventArgs e)
                var model = new Model();
                this.DataContext = model;

    3. Bind the Width-property of the border to the appropriate property of your model like this:

            <Border Width="{Binding BorderWidth}">

    Hope that helps! If not, please do not hesitate to contact me!

    ---------------------------------- Robin Sedlaczek @ Microsoft Forums

    • Marked as answer by Anne Jing Thursday, July 24, 2014 3:30 AM
    Wednesday, July 16, 2014 10:24 AM