如何在XAML编辑器中查看设计时数据绑定(在运行时工作)? [英] How to see design-time data-binding in XAML editor (it works in runtime)?

查看:242
本文介绍了如何在XAML编辑器中查看设计时数据绑定(在运行时工作)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据绑定版本号显示如下:

I data-binded version number to appear as follows:

<Window <!-- ... --> DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <TextBlock>
            Version is: 
            <Run Text="{Binding Version, Mode=OneWay}"></Run>
            and advancing...
        </TextBlock>
    </Grid>
</Window>

,它在运行时工作。

如何在Visual Studio 2012的XAML编辑器中设计时看到它?我只看到:

How can I see it during design-time in the XAML editor in Visual Studio 2012 ? I only see:

Version is: and advancing...

而不是:

Version is: 5.2.2 and advancing...



编辑 - 我的解决方案:



Jure的下面的答案可以工作,但最终使用了一个虚拟视图模型静态代码技术,这对我来说更好,因为数据是真正的视图模型类型的模拟:

EDIT - My solution:

Jure's answer below works, but I ended up using a dummy view-model static code technique, which works better for me since the data is a mock of the real view-model type:

d:DataContext="{Binding Source={StaticResource DesignViewModel}}" ...


推荐答案

简短的回答,你不能这样做。 VS设计器没有执行运行时代码,并且您的绑定将不会在desing时间内解决。
但是通过 d:DesignData 扩展名支持设计时间数据。

Short answer, you can't do it that way. VS designer is not executing runtime code and your binding will not be resolved in desing 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>
</Window>

d:DataContext = {d:DesignData .... 设置将用于解析VS设计器表面中的绑定的设计时间 DataContext 。您可以将其设置为包含示例数据的xaml文件。应该使用DesignData构建操作构建示例xaml文件。

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.

在此处查看更多内容:http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/ sample-data-in-the-wpf-and-silverlight-designer.aspx

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

这篇关于如何在XAML编辑器中查看设计时数据绑定(在运行时工作)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆