我有一个问题要将子类绑定到我的XML文本框,我按照这篇文章来做,但是如果不使用静态类它就无法工作。有没有办法不使用静态类?
我关注这篇文章作为参考。
Binding textbox values to a model in wpf
我的代码是:
public class Model:INotifyPropertyChanged{
public event PropertyChangedEventHandler PropertyChanged;
private string title;
public string Title{
get {
return title;
}
set {
if (tilte!= value) {
tilte= value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class ViewModel{
public Model modelObj;
public ViewModel(){
modelObj= new Model();
this.DataContext = modelObj;
modelObj.Title = "New title"; // <--- this don't update xml
}
}
<Page
x:Class="AppTest.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:m ="using:Models"
xmlns:vm ="using:ViewModels"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
<m:Model></m:Model>
</Page.DataContext>
<Grid>
<TextBlock Text="{Binding Path=Title}"/>
</Grid>
</Page>
答案 0 :(得分:1)
您可以将视图模型设置为数据上下文并绑定到Model.Title。
<强>更新强>
这有效:
<Page x:Class="WpfApplication8.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:m ="using:Models"
xmlns:vm="clr-namespace:WpfApplication8.ViewModels">
<Page.DataContext>
<vm:ViewModel/>
</Page.DataContext>
<Grid>
<TextBlock Text="{Binding ModelObj.Title, TargetNullValue='null', FallbackValue='fallback'}"/>
</Grid>
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Model : BindableBase
{
private string title;
public string Title
{
get
{
return title;
}
set
{
if (title != value)
{
title = value;
NotifyPropertyChanged();
}
}
}
}
public class ViewModel : BindableBase
{
private Model modelObj;
public Model ModelObj
{
get
{
return modelObj;
}
set
{
modelObj = value;
NotifyPropertyChanged();
}
}
public ViewModel()
{
ModelObj = new Model();
ModelObj.Title = "New title";
}
}
答案 1 :(得分:1)
您应该设置您设置为Page的Title
的{{1}}类实例的Model
属性:
DataContext
或者:
<Page.DataContext>
<m:Model Title="New title"></m:Model>
</Page.DataContext>
此外,您不设置视图模型的<Page.DataContext>
<m:ViewModel />
</Page.DataContext>
<Grid>
<TextBlock Text="{Binding Path=modelObj.Title}"/>
</Grid>
属性。您将视图的DataContext
属性设置为视图模型的实例。
修改强>
DataContext
必须是公共属性(而不是字段),以便您能够绑定到它:
modelObj