wpf 如何在 XAML 编辑器中查看设计时数据绑定(它在运行时工作)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16401885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:47:20  来源:igfitidea点击:

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

wpfvisual-studioxamldata-bindingdesign-time

提问by Tar

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>

and it's working during run-time.

它在运行时工作。

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

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

Version is: and advancing...

instead of:

代替:

Version is: 5.2.2 and advancing...

EDIT - My solution:

编辑 - 我的解决方案:

Jure's answer belowworks, 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:

Jure 在下面的回答有效,但我最终使用了一个虚拟的视图模型静态代码技术,这对我来说效果更好,因为数据是真实视图模型类型的模拟:

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

采纳答案by 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:DesignDataextension.

简短的回答,你不能那样做。VS 设计器不执行运行时代码,您的绑定不会在设计时解析。但是通过d:DesignData扩展支持设计时数据。

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 DataContextthat 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.

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

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

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

回答by XAMeLi

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

确保在 xaml 文件的根标记(在您的情况下为 Window 标记)中有这些定义:

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:

然后,您可以在 xaml 中的任何位置(包括根标记)添加以下内容:

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.

如果您需要为设计模式运行特殊逻辑,请查看此答案