且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-02 23:31:28

简而言之,你不能那样做.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一个>