没有静态的绑定文本框 class

Binding textbox without static class

我在将 subclass 绑定到我的 XML 文本框时遇到问题,我按照这个 post 来做,但是如果不使用静态 class。有没有办法不使用静态 class?

我按照这个 post 作为参考。

我的代码是:

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>

您可以将视图模型设置为数据上下文并绑定到 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"; 
    }
}

您应该将 Model class 实例的 Title 属性 设置为页面的 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 必须是 public 属性 (而不是字段),以便您能够绑定到它:

public Model modelObj { get; set; }