简体   繁体   English

同步ComboBox WPF,MVVM

[英]Synchronized ComboBox WPF, MVVM

I'm new to WPF and MVVM as well. 我也是WPF和MVVM的新手。 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'. 它们的值都在14到80之间,并且还有一个元素“ 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'). 在第一个组合框中选择(例如)20时-第二个组合框应更新(值从20到80,以及元素“ 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'). 在第二个组合框中选择(例如)30时-第一个组合框也应更新(值从20到30,以及元素“ Any”)。 If you choose 'Any' - it means values from 14 to 80. 如果您选择“任何”,则表示值介于14到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'). 当我在第一个组合框中选择40时-第二个组合框已更新(40-80 +'Any')。 After it I select 50 in the first combobox - and the second wasn't updated. 之后,我在第一个组合框中选择50-第二个未更新。 I don't understand why. 我不明白为什么。

ViewModelBase.cs using System.ComponentModel; 使用System.ComponentModel的ViewModelBase.cs ;

namespace WpfApplication3.ViewModel
{
    class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}

MainWindowViewModel.cs 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 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 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解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM