2016-07-18 5 views
0

私はモデルを持っています、パターンに従って、モデルのプロパティをViewModelから更新しますか?更新モデルは、例えば

  • 全体モデルを更新しますか?
  • イベントを追加する特定のプロパティを更新しますか?
  • 依存性注入?
+0

これは非常に簡単な例です。プロパティは、ViewModelで合成できます。プロパティはnull可能です。プロパティは、リポジトリなどから取得できます。 – streamdown

+0

このEntierlyは、DataContextの設定方法、およびアプリケーションの処理方法によって異なります。しかし、一般的に、モデル内のプロパティはバインドされているため、そのために更新する必要があります – lokusking

+0

実際のプロジェクトを実行する場合、モデルをコンストラクタViewModelに渡します。しかし、私はリメイクすることができます))私はちょうどパターンに従って適度な方向が必要です。モデルをXAMLのビューに直接バインドして更新してください。間違っていると思います。 – streamdown

答えて

1

これは真の答えではありません。この1は、ちょうどすべてが依存してどのように、関係を示すべきである:

まず、あなたのウィンドウ

<Window x:Class="MySample.MainWindow" 
     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" 
     xmlns:local="clr-namespace:MySample" 
     xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
     mc:Ignorable="d"   
     Title="MainWindow" > 
    <Window.Resources> 
     <BooleanToVisibilityConverter x:Key="BoolToVis"/>  
    </Window.Resources> 

    <Grid Height="200"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <StackPanel> 
      <CheckBox Content="ShowListBox" x:Name="chk"></CheckBox> 
      <ListView ItemsSource="{Binding Ponys}" x:Name="lst" SelectedItem="{Binding SelectedPony}"> 
       <ListView.Visibility> 
        <Binding ElementName="chk" Path="IsChecked" Converter="{StaticResource BoolToVis}"/> 
       </ListView.Visibility> 
       <ListView.ItemTemplate> 
        <DataTemplate DataType="{x:Type local:Pony}"> 
         <WrapPanel Background="{Binding Color}" > 
          <TextBlock Text="{Binding Id}" Margin="0,0,5,0" Padding="2"/> 
          <TextBox Text="{Binding Name}"></TextBox> 
         </WrapPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
      <WrapPanel> 
       <TextBlock Text="New Description for selected Pony: "/> 
       <TextBox Width="100" Text="{Binding SelectedPony.Name, UpdateSourceTrigger=PropertyChanged}"></TextBox> 
      </WrapPanel> 
     </StackPanel> 
    </Grid> 
</Window> 

第二に、モデル

public class Pony : INotifyPropertyChanged 
    { 
     public int Id { 
      get; set; 
     } 
     private string _name; 
     public string Name { 
      get { return this._name; } 
      set { this._name = value; 
       this.OnPropertyChanged("Name"); 
      } 
     } 

     public Brush Color { get; set; } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

第三に、使用

public partial class MainWindow : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public MainWindow() 
     { 
      InitializeComponent(); 
      this.Ponys = new List<Pony>(); 
      this.Ponys.Add(new Pony() { Id = 1, Name = "Fluffy", Color = Brushes.DeepPink }); 
      this.Ponys.Add(new Pony() { Id = 2, Name = "Not so fluffy", Color = Brushes.Chocolate }); 

      this.DataContext = this; 
     } 

     private Pony _pony; 
     public Pony SelectedPony { 
      get { return this._pony; } 
      set { 
       this._pony = value; 
       this.OnPropertyChanged("SelectedPony"); 
      } 
     } 

     public List<Pony> Ponys { get; set; } 
    } 

は、いくつかの注意

  1. 私は理由lazynessのないMainViewModelをしなかったので、私はちょうどあなたが見ての通り、DataTemplatesがバインドされている分離コード
  2. 、純粋なGUIバインディング、モデル操作および使用法を使用しますDataContextのジャンプスタートには十分です
  3. すべての仕組みを理解して理解したい場合は、サードパーティ製品を使用しないことを強くお勧めします。私はこれをとても快適に、何度も何度も何度も入力しないことを知っています。しかしそれはあなたのことを本当に理解していることです。また、あなたは私はあなたがこのFody-ブツを使用していけない場合は、コンストラクタ・インジェクション
  4. の必要がないためにもlazyness
  5. の、どこにでもPropertyChangedを実装didntのブレークポイントを設定し、
  6. に行くいただきました!に深い見てみることができます
+0

ありがとうございます。 "しかし、それは方法です、あなたは何が起こっているかを本当に理解しています。" - OnPropertyChangedの間に何が起こっているのか分かります))これは私がFodyを使う理由の一つです。 – streamdown

+0

前に書いたように:怠惰のため。 – streamdown