私はモデルを持っています、パターンに従って、モデルのプロパティをViewModelから更新しますか?更新モデルは、例えば
- 全体モデルを更新しますか?
- イベントを追加する特定のプロパティを更新しますか?
- 依存性注入?
私はモデルを持っています、パターンに従って、モデルのプロパティをViewModelから更新しますか?更新モデルは、例えば
これは真の答えではありません。この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; }
}
は、いくつかの注意:
MainViewModel
をしなかったので、私はちょうどあなたが見ての通り、DataTemplates
がバインドされている分離コードDataContext
のジャンプスタートには十分ですPropertyChanged
を実装didntのブレークポイントを設定し、ありがとうございます。 "しかし、それは方法です、あなたは何が起こっているかを本当に理解しています。" - OnPropertyChangedの間に何が起こっているのか分かります))これは私がFodyを使う理由の一つです。 – streamdown
前に書いたように:怠惰のため。 – streamdown
これは非常に簡単な例です。プロパティは、ViewModelで合成できます。プロパティはnull可能です。プロパティは、リポジトリなどから取得できます。 – streamdown
このEntierlyは、DataContextの設定方法、およびアプリケーションの処理方法によって異なります。しかし、一般的に、モデル内のプロパティはバインドされているため、そのために更新する必要があります – lokusking
実際のプロジェクトを実行する場合、モデルをコンストラクタViewModelに渡します。しかし、私はリメイクすることができます))私はちょうどパターンに従って適度な方向が必要です。モデルをXAMLのビューに直接バインドして更新してください。間違っていると思います。 – streamdown