I'm new to WPF and MVVM as well. And also sorry for my bad English. Thank you.
I need to create two synchronized comboboxes. Both of them have values from 14 to 80 and also an element 'Any'. When in the first combobox you choose (for example) 20 - the second combobox should be updated (values from 20 to 80 and also the element 'Any'). When in the second combobox you choose (for example) 30 - the first combobox should be updated as well (values from 20 to 30 and also the element 'Any'). If you choose 'Any' - it means values from 14 to 80.
So, i tried to do it. And it works, but very poorly. When I select 40 in the first combobox - the second combobox was updated (40-80 + 'Any'). After it I select 50 in the first combobox - and the second wasn't updated. I don't understand why.
ViewModelBase.cs using System.ComponentModel;
namespace WpfApplication3.ViewModel
{
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
MainWindowViewModel.cs
namespace WpfApplication3.ViewModel
{
class MainWindowViewModel : ViewModelBase
{
private static readonly int MinAge = 14;
private static readonly int MaxAge = 80;
private static readonly string Any = "Any";
private List<string> _fromValues = null;
private List<string> _toValues = null;
private string _from = null;
private string _to = null;
public List<string> FromValues
{
get
{
if ( _fromValues == null )
_fromValues = new List<string>();
return _fromValues;
}
}
public List<string> ToValues
{
get
{
if ( _toValues == null )
_toValues = new List<string>();
return _toValues;
}
}
public string From
{
get { return _from; }
set
{
if ( _from == value )
return;
_from = value;
OnPropertyChanged(nameof(From));
int min = _from == Any ? MinAge : int.Parse(_from);
int max = MaxAge;
ToValues.Clear();
for ( int i = min; i <= max; i++ )
ToValues.Add(i.ToString());
ToValues.Add(Any);
OnPropertyChanged(nameof(ToValues));
}
}
public string To
{
get { return _to; }
set
{
if ( _to == value )
return;
_to = value;
OnPropertyChanged(nameof(To));
int min = MinAge;
int max = _to == Any ? MaxAge : int.Parse(_to);
FromValues.Clear();
FromValues.Add(Any);
for ( int i = min; i <= max; i++ )
FromValues.Add(i.ToString());
OnPropertyChanged(nameof(FromValues));
}
}
public MainWindowViewModel()
{
FromValues.Add(Any);
for (int i = MinAge; i <= MaxAge; i++ )
{
FromValues.Add(i.ToString());
ToValues.Add(i.ToString());
}
ToValues.Add(Any);
From = To = Any;
}
}
}
MainWindow.xaml.cs
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = new ViewModel.MainWindowViewModel();
InitializeComponent();
}
}
}
MainWindow.xaml
<Window x:Class="WpfApplication3.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:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Column="0" IsEditable="False" ItemsSource="{Binding FromValues}" SelectedItem="{Binding From}"/>
<ComboBox Grid.Column="2" IsEditable="False" ItemsSource="{Binding ToValues}" SelectedItem="{Binding To}"/>
</Grid>
</Window>
正如@Boopesh所建议的,ObservableCollection解决了我的问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.