Tar*_*Tar 26 data-binding wpf xaml design-time visual-studio
我的数据绑定版本号如下所示:
<Window <!-- ... --> DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock>
Version is:
<Run Text="{Binding Version, Mode=OneWay}"></Run>
and advancing...
</TextBlock>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
它在运行期间正在运行.
如何在Visual Studio 2012的XAML编辑器中在设计时看到它?我只看到:
Version is: and advancing...
Run Code Online (Sandbox Code Playgroud)
代替:
Version is: 5.2.2 and advancing...
Run Code Online (Sandbox Code Playgroud)
下面的Jure的答案有效,但我最终使用了虚拟视图模型静态代码技术,这对我来说更好,因为数据是真实视图模型类型的模拟:
d:DataContext="{Binding Source={StaticResource DesignViewModel}}" ...
Run Code Online (Sandbox Code Playgroud)
XAM*_*eLi 45
确保在xaml文件的根标记处具有这些定义(在您的情况下为Window标记):
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Run Code Online (Sandbox Code Playgroud)
然后,在xaml中的任何位置(包括根标签),您可以添加:
d:DataContext="{d:DesignInstance myNamespace:MyViewModel, IsDesignTimeCreatable=True}"
Run Code Online (Sandbox Code Playgroud)
现在,您只需要确保在构造函数中初始化值或具有属性的默认值.
如果您需要为设计模式运行特殊逻辑,请查看此答案.
jur*_*ure 18
简短的回答,你不能这样做.VS设计器不执行运行时代码,并且在设计时不会解析绑定.但是通过d:DesignData扩展支持设计时数据.
您可以这样设置设计数据上下文:
<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>
Run Code Online (Sandbox Code Playgroud)
d:DataContext={d:DesignData....设置DataContext将用于解析VS设计器表面中的绑定的设计时间.您可以将其设置为包含示例数据的xaml文件.应使用"DesignData"构建操作构建示例xaml文件.
点击此处查看更多信息:http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer.aspx