How to see design-time data-binding in XAML editor (it works in runtime)?

落爺英雄遲暮 提交于 2019-11-27 21:08:51
jure

Short answer, you can't do it that way. VS designer is not executing runtime code and your binding will not be resolved in design time. But there is support for design time data through d:DesignData extension.

You can set design data context this way:

<Window xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DataContext="{d:DesignData Source=/SampleData/SomeSampleData.xaml}"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <TextBlock>
        Version is: 
        <Run Text="{Binding Version, Mode=OneWay}"></Run>
        and advancing...
    </TextBlock>
</Grid>

d:DataContext={d:DesignData.... sets the desing time DataContext that will be used to resolve bindings in VS designer surface. You can set it to a xaml file that contains your sample data. Sample xaml file should be built with "DesignData" build action.

See more here: http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx

XAMeLi

Make sure that you have these definitions at the root tag of your xaml file (in your case the Window tag):

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"

Then, anywhere in the xaml (including the root tag) you can add this:

d:DataContext="{d:DesignInstance myNamespace:MyViewModel, IsDesignTimeCreatable=True}"

Now you just need to make sure that you initialize the values in a constructor or have default values for properties.

If you need to run a special logic for design mode, look at this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!