none
x:Bind and design-time data RRS feed

  • Question

  • I love the new compiled bindings and the error checking and performance they provide. However, I haven't been able to find info on how to use design-time data with them.

    For example, with the old bindings system, I would set the DataContext twice. First with something like


    d:DataContext="{d:DesignData Source=/Design/MainPageData.xaml}

    Then I would set the actual DataContext to the real view model. My bindings worked for both runtime and design-time. The problem, however, is that x:Bind defaults to the page (instead of DataContext) and there is (apparently) no way to have them bind to some design-time data of my choice.

     I tried that to see if I could check DesignMode.DesignMode and instantiate a view model in the code-behind that would be different for run-time and design-time, with no luck... The code-behind code doesn't even seem to be running in the designer: I tried to intentionally throw an unhandled exception in the page's constructor and the designer seems unphased.

    Ideas?


    • Edited by Mr. Pete Monday, July 13, 2015 8:08 PM
    Monday, July 13, 2015 8:07 PM

Answers

  • Hi Pete,

    Theoretically speaking, I'm not sure if x:Bind can do the design time binding.

    Base on my understanding for the x:Bind, it generate codes to boost the Apps' performance, so the code are generated when we build the app instead of the runtime or design time. Besides, the d: namespace comes from Blend instead of original xaml schema, I think Blend does not implement the d: for x:Bind at this moment.

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    See this: https://channel9.msdn.com/Events/Build/2015/3-635 for more information about x:Bind.

    Perhaps you could feedback at uservoice to see if our Visual Studio Team will work on this: https://visualstudio.uservoice.com/forums/121579-visual-studio

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 28, 2015 9:54 AM

All replies

  • You cannot set a design time DataContext for compile-time bindings. In fact you cannot set a DataContext for the binding at all. But you could use the FallbackValue property to provide design-time data:

    <TextBlock Text="{x:Bind SomeProperty, FallbackValue=test...}"/>

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, July 13, 2015 9:48 PM
  • I don't think this will work, as we have complex data types being bound, including converters.

    The designer used to execute the code in the page's constructor. It no longer appears to do so. Is there a way to turn this on?

    It would be a bummer if we have to choose between compiled bindings and designer-friendly architecture.

    Tuesday, July 14, 2015 6:17 PM
  • Hi Pete,

    Theoretically speaking, I'm not sure if x:Bind can do the design time binding.

    Base on my understanding for the x:Bind, it generate codes to boost the Apps' performance, so the code are generated when we build the app instead of the runtime or design time. Besides, the d: namespace comes from Blend instead of original xaml schema, I think Blend does not implement the d: for x:Bind at this moment.

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    See this: https://channel9.msdn.com/Events/Build/2015/3-635 for more information about x:Bind.

    Perhaps you could feedback at uservoice to see if our Visual Studio Team will work on this: https://visualstudio.uservoice.com/forums/121579-visual-studio

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 28, 2015 9:54 AM
  • Below is the only way I found.

    public class MainPageBase : Page
    {
        public MainPageBase()
        {
            if (DesignMode.DesignModeEnabled)
            {
                DataContext = CreateDesignModeViewModel();
            }
        }
     
        public MyViewModel ViewModel
        {
            get
            {
                if (DataContext == null)
                {
                    DataContext = CreateRuntimeViewModel();
                }
     
                return (MyViewModel)DataContext;
            }
        }
     
        private MyViewModel CreateDesignModeViewModel()
        {
            return ...;
        }
     
        private MyViewModel CreateRuntimeViewModel()
        {
            return ...;
        }
    }
     
    public sealed partial class MainPage : MainPageBase
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
    }
    

    Sunday, September 13, 2015 10:04 AM