我使用 Wpf 4.5 和 Caliburn Micro 2.0.2。
我想将文本框绑定(bind)到 View 模型的属性。该属性(称为 ResultData)是 TextXmlData 类中的一个对象。该类是从 xsd 自动生成的类。我使用 Microsoft Xsd.exe 来制作它。
这是 View 模型
public class ShellViewModel : PropertyChangedBase, IHaveDisplayName
{
public string DisplayName { get; set; }
private TestXmlData _resultData;
public TestXmlData ResultData
{
get { return _resultData; }
set
{
_resultData = value;
NotifyOfPropertyChange(() => _resultData);
}
}
public ShellViewModel()
{
DisplayName = "Shell Window";
}
public void CreateObject()
{
String xmlData = "<TestXmlData><Id>88</Id><Name>What a name</Name></TestXmlData>";
if (ResultData == null) { ResultData = new TestXmlData(); }
XmlSerializer oXmlSerializer = new XmlSerializer(ResultData.GetType());
ResultData = (TestXmlData)oXmlSerializer.Deserialize(new StringReader(xmlData));
// at this point the debugger shows that the ResultData is correctly filled,
// the Name is definitely not empty
}
}
这是 View
<UserControl x:Class="CMWpfXmlSerializer2Ways.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid Width="300" Height="300">
<StackPanel Width="200"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Button x:Name="CreateObject"
Width="190"
Content="Create Object from XML" />
<TextBox Width="190"
DataContext="{Binding ResultData}"
Text="{Binding Name}" />
</StackPanel>
</Grid>
</UserControl>
并且 TextBox 始终显示为空!
我也尝试过使用 Text="{Binding ResultData.Name}",但 TextBox 仍然显示为空。
任何人都可以帮助并告诉我上面的代码有什么问题吗? 请随时修改代码。 提前致谢。
最佳答案
ResultData 是 ViewModel 的一个属性。因此,您需要在较高级别将 ViewModel 设置为 DataContext,然后您可以在较低级别将其属性用作绑定(bind)源。
为了运行您的示例,我做了一些更改并像下面这样运行:
<TextBox x:Name="tbName" DataContext="{Binding ResultData}" Text="{Binding Name}" />
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ShellViewModel vm = new ShellViewModel();
vm.CreateObject();
this.DataContext = vm;
}
...
///
public class ShellViewModel : INotifyPropertyChanged
{
public string DisplayName { get; set; }
private TestXmlData _resultData;
public TestXmlData ResultData
{
get { return _resultData; }
set
{
_resultData = value;
OnPropertyChanged("ResultData");
}
}
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public ShellViewModel()
{
DisplayName = "Shell Window";
}
public void CreateObject()
{
String xmlData = "<TestXmlData><Id>88</Id><Name>What a name</Name></TestXmlData>";
if (ResultData == null) { ResultData = new TestXmlData(); }
XmlSerializer oXmlSerializer = new XmlSerializer(ResultData.GetType());
ResultData = (TestXmlData)oXmlSerializer.Deserialize(new StringReader(xmlData));
// at this point the debugger shows that the ResultData is correctly filled,
// the Name is definitely not empty
}
public event PropertyChangedEventHandler PropertyChanged;
}
关于c# - Wpf + Caliburn 微 : Binding Textbox to a custom object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913658/