例如,我有一个模型:
public class PropertiesModel
{
public bool MaterialEnable { get; set; }
}
视图模型:
public class ViewModel: INotifyPropertyChanged
{
private PropertiesModel _model;
public bool Recalc { get; set; }
puplic ViewModel (PropertiesModel model)
{
_model = model;
Recalc = _model.MaterialEnabled;
}
}
最后 - XAML:
<ToggleSwitch IsChecked="{Binding Recalc}" />
根据模式,如何正确地从ViewModel更新模型中的属性?
答案 0 :(得分: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
,所以我只使用了CodeBehind DataTemplates
,纯GUI绑定,模型操作和DataContext
的用法。足够的快速启动PropertyChanged
的任何地方实施,也因为懒惰