gpt4 book ai didi

c# - Wpf + Caliburn 微 : Binding Textbox to a custom object

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:00 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com