本文介绍了XAML数据绑定类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对WPF中的数据绑定有疑问。从Microsoft的文档,这里: http://msdn.microsoft.com/en-us/library/ms752347。 aspx [ ^ ]那里是关于源类的东西。 < DockPanel xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x = http://schemas.microsoft.com/winfx / 2006 / xaml xmlns:c = clr-namespace:SDKSample > < DockPanel.Resources > < c:MyData x:Key = myDataSource / > < / DockPanel.Resources > < DockPanel.DataContext > < 绑定 Source = {StaticResource myDataSource} / > < / DockPanel.DataContext > < 按钮 背景 = {Binding Path = ColorName} 宽度 = 150 高度 = 30 > 我一定是RED!< / Button > < / DockPanel > 这里我们有一个MyData的源类。但仅作为一个类,它没有被实例化。所以我们不知道它的Binding Path = ColorName是否为红色。但它为按钮提供了红色。所以我的问题是如何在没有实例化的情况下发生这种情况?这是一个静态类还是我们有一个代码隐藏,微软没有显示实例化MyData?解决方案 当XAML被处理/执行时,它正在实例化所有类。 < DockPanel> ...< / DockPanel> 被实例化, 所以< Button> ...< /按钮> 以及< c:MyData x:Key =myDataSource/> !! Hi, i have a question about data binding in WPF. From Microsoft's documentation,here: http://msdn.microsoft.com/en-us/library/ms752347.aspx[^] there is something that bugs me about the source class. <DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:SDKSample"> <DockPanel.Resources> <c:MyData x:Key="myDataSource"/> </DockPanel.Resources> <DockPanel.DataContext> <Binding Source="{StaticResource myDataSource}"/> </DockPanel.DataContext> <Button Background="{Binding Path=ColorName}" Width="150" Height="30">I am bound to be RED!</Button></DockPanel>Here we have a source class of MyData. But as a class alone it's not instantiated. So we don't know if it's Binding Path = ColorName is red. But it gives the color red to the button. So my question is how did that happen without instantiation? Was that a static class or we have a code behind that Microsoft didn't show that instantiates MyData? 解决方案 When the XAML is processed/executed, it is instantiating all of the classes.The <DockPanel> ... </DockPanel> is instantiated, so is the <Button>...</Button>and so is the <c:MyData x:Key="myDataSource"/>!! 这篇关于XAML数据绑定类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 14:20