2017-02-19 57 views
-1

我有一個問題,將一個子類綁定到我的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> 

回答

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"; 
    } 
} 
+0

你的意思。 我已經嘗試並且無法工作。 – Deimon

+0

作爲魅力!我也想在視圖模型中放入INotifyPropertyChanged接口。謝謝你,工作很好。 – Deimon

1

您應該設置您設置爲頁面的DataContextModel類的實例的Title財產:

<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 Model modelObj { get; set; } 
+0

奇怪......在這兩種方式中,我得到一個空的texblock。 – Deimon

+0

您是否在其他位置設置了頁面的DataContext,例如在Demo.xaml.cs中? – mm8

+0

哦,是的工作!我忘記了在MainPage.xaml.cs中設置DataContext 'ViewModel vm = new ViewModel(); 的DataContext = vm.modelObj;' '' 但是,如果我把'的DataContext = vm'in的MainPage和''作爲你的例子XAML不起作用。 – Deimon