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

查看:32
本文介绍了如何在 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 设计器不执行运行时代码,您的绑定不会在设计时解析.但是通过 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 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.... 设置设计时间 DataContext 将用于解析 VS 设计器界面中的绑定.您可以将其设置为包含示例数据的 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

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

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