根据按钮单击更改组合框值c#wpf

时间:2019-04-18 08:33:39

标签: c# wpf mvvm combobox relaycommand

我正在尝试从WinForm过渡到WPF,在研究此主题时,我遇到了这一挑战,

  

在单击按钮时更改组合框项目,然后创建一个按钮   显示原始所选值”

我设法使用内联RelayCommand来做到这一点,但是在我看来,我正在破坏MVVM模式,而且它不符合那部分“显示原始选定值的按钮”,因此我继续阅读后发现我可以使用IValueConverter。但是我不知道在按钮单击和转换器之间进行连接(这是否有可能,或者我需要做其他事情?)

我的viewModel

 class MainWindowVM : INotifyPropertyChanged
    {
        public MainWindowVM()
        {
            CBItems = new ObservableCollection<double>();
            for (int i = 985; i <= 1030; i++)
                CBItems.Add(i);
            CBSelectedValue = 1013;

            testCmd = new RelayCommand(p =>
            {
                for (int i = 0; i < CBItems.Count; i++)
                { 
                    if(p.Equals("inHg"))
                    {
                        CBItems[i] *= 33.86;
                        ConversionUnit = "milibar";
                    }
                    else
                    {
                        CBItems[i] /= 33.86;
                        ConversionUnit = "inHg";
                    }
                }
            }
        );

            SetQNH = new RelayCommand(p => MessageBox.Show(string.Format("QNH = {0}", Convert.ToInt32(p))));

        }

        public RelayCommand SetQNH { get; set; }         
        public RelayCommand testCmd { get; set; }


        private ObservableCollection<double> _CBItems;
        public ObservableCollection<double> CBItems
        {
            get { return _CBItems; }
            set
            {
                _CBItems = value;
                OnPropertyChanged("CBItems");
            }
        }

        private string _conversionUnit;

        public string ConversionUnit
        {
            get { return _conversionUnit; }
            set
            {
                _conversionUnit = value;
                OnPropertyChanged("ConversionUnit");
            }
        }

        private int _CBSelectedValue;
        public int CBSelectedValue {
            get { return _CBSelectedValue; }
            set
            {
                _CBSelectedValue = value;
                OnPropertyChanged("CBSelectedValue");                  
            }
        }

        private void OnPropertyChanged(string s)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(s));
        }

        public event PropertyChangedEventHandler PropertyChanged;             
    }
}

我的窗口

<Window x:Class="QNH.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:QNH"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        mc:Ignorable="d"
        Title="MainWindow" Height="318" Width="638">
    <Window.Resources>
        <local:IUnitsValueConverter x:Key="MyConverter" />
    </Window.Resources>
    <Window.DataContext>
        <local:MainWindowVM/>
    </Window.DataContext>
    <Grid>
        <ComboBox  Name="cb" ItemsSource="{Binding CBItems}" SelectedItem="{Binding CBSelectedValue}" HorizontalAlignment="Left" Margin="406,136,0,0" VerticalAlignment="Top" Width="120" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource MyConverter} }"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>


        <Button Name="Unit" Content="{Binding ConversionUnit, TargetNullValue=inHg}"
                Command="{Binding testCmd}" CommandParameter="{Binding ElementName=Unit, Path=Content}"
                HorizontalAlignment="Left" Margin="140,136,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="SET" Command="{Binding SetQNH}" CommandParameter="{Binding ElementName=cb, Path=SelectedValue}" HorizontalAlignment="Left" Margin="406,163,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

和我的转换器

[ValueConversion(typeof(int), typeof(double))]
    public class IUnitsValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //var val = (int?)value;
            //if (val == null)
            //    return -1;

            return value;//* 33.86;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

0 个答案:

没有答案