2017-09-23 9 views
0

MVVMの仕組みを理解して、簡単に始めたいと思います。Bindingを使ってテキストボックスにプロパティを入力してくださいMVVM

モデル、ビュー、およびビューモデルを作成しました。私は窓の上に眺めを張った。これは多かれ少なかれ動作します。ビューの 私はプロパティの値で塗りつぶしたいテキストボックスを持っています。テキストボックスは空のままです!

これは私が持っているものです: モデル:

namespace Qbox_0001.Model 
{ 
    public class RegistratieModel 
    { 
    } 

    public class Registratie : INotifyPropertyChanged 
    { 

     //Fields 
     private string programmaNaam; 

     //eventhandler die kijkt of een property wijzigt 
     public event PropertyChangedEventHandler PropertyChanged; 

     private void RaisePropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
      } 
     } 

     //Properies 
     public string ProgrammaNaam 
     { 
      get { return programmaNaam; } 
      set 
      { 
       if (programmaNaam != value) 
       { 
        programmaNaam = value; 
        RaisePropertyChanged("ProgrammaNaam"); 
       } 
      } 
     } 

    } 
} 

ビュー:View.csで

<UserControl x:Name="UserControlRegistratie" x:Class="Qbox_0001.Views.RegistratieView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Qbox_0001.Views" 
      mc:Ignorable="d" Height="1000" Width="860" MaxWidth="860" HorizontalContentAlignment="Left" VerticalContentAlignment="Top"> 
    <Grid x:Name="GridRegistratie"> 


     <Grid.RowDefinitions> 
      <RowDefinition x:Name="RowDef_Menu" Height="21*" MaxHeight="21" MinHeight="21"/> 
      <RowDefinition x:Name="RowDef_TabControl" Height="500*" MinHeight="500"/> 
      <RowDefinition x:Name="Rowdef_Bottom" Height="40*" MaxHeight="40" MinHeight="40"/> 
     </Grid.RowDefinitions> 

     <Grid x:Name="Grid_Registratie_WorkArea" Grid.Row="1"> 

      <TabControl x:Name="TabControl_Registratie"> 
       <TabItem Header="Registratie"> 
        <Grid x:Name="Grid_Tab_Registratie"> 
         <Border> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition x:Name="GridRowDef_Algemeen" MinHeight="68" Height="68*" MaxHeight="68"/> 
            <RowDefinition x:Name="GridRowDef_Locatie" MinHeight="120" Height="120*" MaxHeight="120"/> 
            <RowDefinition x:Name="GridRowDef_AantalDagen" MinHeight="105" Height="105*" MaxHeight="105"/> 
            <RowDefinition x:Name="GridRowDef_MaxDagen" MinHeight="105" Height="105*" MaxHeight="105"/> 
            <RowDefinition x:Name="GridRowDef_Lokaal" MinHeight="100" Height="100*" MaxHeight="100"/> 
            <RowDefinition x:Name="GridRowDef_LicBestand" Height="150*" MaxHeight="150" MinHeight="150"/> 
           </Grid.RowDefinitions> 

           <GroupBox x:Name="GroupBox_algemeen" Header="Algemeen" Margin="10,4,10,3"> 
            <Grid> 
             <Label x:Name="Label_Klant" Content="Klant:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Padding="0,5,5,5"/> 
             <Label x:Name="Label_Programma" Content="Programma:" HorizontalAlignment="Left" Margin="356,10,0,0" VerticalAlignment="Top"/> 
             <Label x:Name="Label_Versie" Content="Versie:" HorizontalAlignment="Left" Margin="645,10,0,0" VerticalAlignment="Top"/> 
             <TextBox x:Name="textBox_KlantNaam" HorizontalAlignment="Left" Height="23" Margin="49,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/> 

             <!-- the textbox keeps empty --> 
             <TextBox x:Name="TextBox_ProgrammaNaam" Text="{Binding ElementName=RegistratieViewModel, Path=ProgrammaNaam, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="431,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="190" IsEnabled="False" /> 


             <TextBox x:Name="TextBox_Versie" HorizontalAlignment="Left" Height="23" Margin="695,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsEnabled="False" /> 

            </Grid> 
           </GroupBox> 

          </Grid> 
         </Border> 
        </Grid> 
       </TabItem> 
       <TabItem Header="Licentie(s)"> 
        <Grid x:Name="Grid_Tab_Licentie" Background="#FFE5E5E5"/> 
       </TabItem> 
      </TabControl> 
     </Grid> 

    </Grid> 
</UserControl> 

namespace Qbox_0001.Views 
{ 
    /// <summary> 
    /// Interaction logic for RegistratieView.xaml 
    /// </summary> 
    public partial class RegistratieView : UserControl 
    { 
     public RegistratieView() 
     { 
      InitializeComponent(); 
      this.DataContext = new Qbox_0001.ViewModel.RegistratieViewModel(); 
     } 
    } 
} 

のViewModel

using Qbox_0001.Model;     // 
using System.Collections.ObjectModel; // 


namespace Qbox_0001.ViewModel 
{ 
    public class RegistratieViewModel 
    { 
     public RegistratieViewModel() 
     { 
      loadRegistratieOnderdelen(); 
     } 

     public ObservableCollection<Registratie> RegistratieOnderdelen //Registratie = "public class Registratie : INotifyPropertyChanged" van de Model 
     { 
      get; 
      set; 
     } 

     public void loadRegistratieOnderdelen() 
     { 
      ObservableCollection<Registratie> regOnderdelen = new ObservableCollection<Registratie>(); 

      regOnderdelen.Add(new Registratie { ProgrammaNaam = "Test" }); 

     } 
    } 
} 

答えて

0

あなたのコードにはいくつかの問題があります。

ローカルObservableCollection(loadRegistratieOnderdelen()メソッド内)にデータを設定していますが、ローカルなので、DataContextのメンバーではないため、ビューでは使用できません。 RegistratieViewModelで既に宣言しているRegistratieOnderdelenのような公開プロパティを使用する必要があります。

次に、ObservableCollectionを使用していますが、String型のプロパティを使用したい場合があります。コレクションは、ListViewやItemsControlなどのリストを表現する場合に使用されます。ビューでは、単一のテキスト値をバインドして、String型のパブリックプロパティがより理にかなっていることを示しています。

ベスト、ニコ

+0

は、初心者のための良いチュートリアルはありますか? – Hansvb

+0

@Hansvb行き、Rachel Limのブログを参照してください。初心者のために詳しく説明し、簡単な例を示します。 https://rachel53461.wordpress.com/ – Bojje

0

DataContextRegistratieViewModelです。このクラスには、Registratieオブジェクトのコレクションを返すRegistratieOnderdelenプロパティがあります。

あなたはそのようなアイテムのProgrammaNaamプロパティにバインドすることができますが、あなたはにバインドするためにどの項目を指定する必要があり、例えば最初の1:

<TextBox x:Name="TextBox_ProgrammaNaam" Text="{Binding Path=RegistratieOnderdelen[0].ProgrammaNaam, UpdateSourceTrigger=PropertyChanged}" ... />