在遵循WPF的MVVM架构,学习WPF DataBindings。我有一个在运行时实例化的对象的单个实例,窗口资源中的XAML代码<p:MemoryPersistentStorageBridge x:Key="persistentMemoryBridge" />
。我试图从对象实例中获取数据,并将其放到TextBox中作为示例,但我没有在该文本框中获取任何文本。MVVM WPF DataBinding疑难解答
XAML:
<Window x:Class="UserConsole.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:PollPublicDataStock;assembly=PollPublicDataStock"
xmlns:local="clr-namespace:UserConsole"
Title="MainWindow" Height="900" Width="800">
<Window.Resources>
<p:MemoryPersistentStorageBridge x:Key="persistentMemoryBridge" />
</Window.Resources>
<Grid Name="grid1" >
<!-- layout defintions -->
<TextBox DataContext="{StaticResource persistentMemoryBridge}" Text="{Binding Path=GetConnectionString}" Margin="0,327,31,491" Foreground="Black" Background="Yellow"/>
</Grid>
</Window>
代码隐藏:
public class MemoryPersistentStorageBridge {
public MemoryPersistentStorageBridge() {
}
public string GetConnectionString() {
return "THISTEXTSHOULDAPPEARINTEXTBOXBUTSADLYDOESNOT";
}
}
快速查看'输出'窗口中将显示绑定错误(s)。 –