如何使用ICommand将组合框事件绑定到ViewModel

我对MVVM比较陌生,我想将我的视图绑定到视图模型。我有很多代码可以从CodeBehind移到ViewModel类中。

我想做的是将ComboBox事件绑定到相应的ViewModel ICommand方法。我希望ComboBox在加载视图和进行选择时显示“ CompanyB”,ComboBox应该给我“ CompanyA”,“ CompanyB”和“ CompanyC”作为选择。

选择公司后,下面2个文本框的值

  

Nachbest.Empf_Ansprechpartner

     

Nachbest.Empfaenger_Mail

必须相应地更改。

问题出在我的代码中,组合框和文本框都为空,组合框内也没有其他选择。

您能帮我找到我在这里想念的吗?预先感谢您的帮助!

XAML(neueNachbestellung.xaml):

<Window xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    <Grid>
        <StackPanel Grid.Column="0" Margin="25,25,0" x:Name="leftStPnl">
            <ComboBox x:Name="cboxEmpfaenger" 
                      ItemsSource="{Binding Empf}" 
                      Text="{Binding Empfaenger}" 
                      FontSize="12" Width="150" Margin="118,0"                      
                      SelectedItem="{Binding SelValue}">
            </ComboBox>
            <TextBox x:Name="txtEmpfAnsprechpartner" Text="{Binding Empf_Ansprechpartner}" FontSize="12" IsEnabled="False" Width="150" Margin="50,0"/>    
            <TextBox x:Name="txtEmpfmail" Text="{Binding Empfaenger_Mail}" FontSize="12" IsEnabled="False" Width="150" Margin="73,0"/>
        </StackPanel>
    </Grid>
</Window>

背后的代码(neueNachbestellung.xaml.cs):

public neueNachbestellung(string someId) 
{
    InitializeComponent();
    this.DataContext = new neueNachbestellungViewModel(someId);
}

视图模型(neueNachbestellungViewModel.cs)

public class neueNachbestellungViewModel: INotifyPropertyChanged
{
public ICommand LoadCombobox => new DelegateCommand<object>(ExecuteLoadCombobox);
public ICommand ComboboxSelectionChanged => new DelegateCommand<object>(ExecuteComboboxSelectionChanged);
public Nachbestellung Nachbest { get; set; }
private object someObject;
private ObservableCollection<string> _empf;
        public ObservableCollection<string> Empf
        {
            get { return _empf; }
            set
            {
                _empf = value;
                OnPropertyChanged("Empf");
            }
        }
        private string _selValue = "12";
        public string SelValue  
        {
            get { return _selValue; }
            set
            {
                _selValue = value;
                OnPropertyChanged("SelValue");
            }
        }

 public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
            }
        }

public neueNachbestellungViewModel(string id)
{

    this.Artikel = new ArtikelViewModel();
    this.ArtikelList = new ObservableCollection<Artikel>();
    InitializeReorderModel(id);    
    ExecuteComboboxSelectionChanged(someObject);                        
}

public void InitializeReorderModel(string id)
{
    //set the MODEL
    this.Nachbest = new Nachbestellung();

    //Retrieve and set some values on *VIEW LOAD*!
    var dbOracle = new Datenbank();
    this.Nachbest.Bv = dbOracle.GetBauvorhaben(hv);
    this.Nachbest.Hv = hv;
    this.Nachbest.Bauleiter = dbOracle.GetBauleiter(hv);
    this.Nachbest.Projektleiter = dbOracle.GetProjektleiter(hv);
}

private void ExecuteLoadCombobox(object param)
{
    Empf = new ObservableCollection<string>()
    {
        "CompanyA","CompanyB","CompanyC"         
    };

    //Company B is the standard selection on combobox load                     
    Nachbest.Empf_Ansprechpartner = "CompanyB";
    Nachbest.Empfaenger_Mail = "orders@companyB.com";
}

private void ExecuteComboboxSelectionChanged(object param)
{
    Empf = new ObservableCollection<string>()
    {
        "CompanyA","CompanyC"             
    };

    switch (SelValue)
    {

        case "CompanyA":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyA";
                Nachbest.Empfaenger_Mail = "service@companyA.com";
            }
            break;

        case "CompanyB":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyB";
                Nachbest.Empfaenger_Mail = "orders@companyB.com";
            }
            break;

        case "CompanyC":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyC";
                Nachbest.Empfaenger_Mail = "info@companyC.com";
            }
            break;

        default:
            MessageBox.Show("Something went wrong with the company selection!");
            break;
    }
}
}

查看片段

如何使用ICommand将组合框事件绑定到ViewModel

qq376055355 回答:如何使用ICommand将组合框事件绑定到ViewModel

这是我快速又肮脏的解决方案。它完成了它所需的工作,我的代码中不再包含这些click_button,selection_changed事件,而是在视图模型中。这就是我现在所需要的。显然,这不是一个优雅的解决方案,但它正在起作用。希望以后能为遇到类似问题的开发人员提供帮助。附带说明:在这种情况下,视图模型内的ICommand属性不是必需的,但我使用它们来处理视图中的按钮单击事件。如果您在应用程序中不需要DelegateCommand类,则可以用自己的属性替换它们。

XAML(neueNachbestellung.xaml):

<Window>
    <Grid>
        <StackPanel Grid.Column="0" Margin="25,25,0" x:Name="leftStPnl">
            <ComboBox x:Name="cboxEmpfaenger" 
                      ItemsSource="{Binding Empf}" 
                      Text="{Binding Empfaenger}" 
                      FontSize="12" Width="150" Margin="118,0"                      
                      SelectedItem="{Binding SelValue}">
            </ComboBox>
            <TextBox x:Name="txtEmpfAnsprechpartner" DataContext="{Binding Nachbest}" Text="{Binding Empf_Ansprechpartner}" FontSize="12" IsEnabled="False" Width="150" Margin="50,0"/>    
            <TextBox x:Name="txtEmpfMail" DataContext="{Binding Nachbest}" Text="{Binding Empfaenger_Mail}" FontSize="12" IsEnabled="False" Width="150" Margin="73,0"/>
        </StackPanel>
    </Grid>
</Window>

背后的代码(neueNachbestellung.xaml.cs):

public neueNachbestellung(string someId) 
{
    InitializeComponent();
    this.DataContext = new neueNachbestellungViewModel(someId);
}

neueNachbestellungViewModel.cs:

public class neueNachbestellungViewModel: INotifyPropertyChanged
{
//public ICommand LoadCombobox => new DelegateCommand<object>(ExecuteLoadCombobox);
public ICommand ComboboxSelectionChanged => new DelegateCommand<object>(ExecuteComboboxSelectionChanged);
public Nachbestellung Nachbest { get; set; }
private object someObject; //DelegateCommand.cs requires an argument
private ObservableCollection<string> _empf;
        public ObservableCollection<string> Empf
        {
            get { return _empf; }
            set
            {
                _empf = value;
                OnPropertyChanged("Empf");
            }
        }
        private string _selValue = "CompanyB"; //default value
        public string SelValue  
        {
            get { return _selValue; }
            set
            {
                _selValue = value;
                OnPropertyChanged("SelValue");
 switch (SelValue)
    {

        case "CompanyA":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyA";
                Nachbest.Empfaenger_Mail = "service@companyA.com";
            }
            break;

        case "CompanyB":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyB";
                Nachbest.Empfaenger_Mail = "orders@companyB.com";
            }
            break;

        case "CompanyC":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyC";
                Nachbest.Empfaenger_Mail = "info@companyC.com";
            }
            break;

        default:
            MessageBox.Show("Something went wrong with the company selection!");
            break;
    }
//setting the Empfaenger property here with the current selected value is necessary for the database insert later on!
Nachbest.Empfaenger = SelValue;
            }
        }

 public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
            }
        }

public neueNachbestellungViewModel(string id)
{

    this.Artikel = new ArtikelViewModel();
    this.ArtikelList = new ObservableCollection<Artikel>();
    InitializeReorderModel(id);    
    ExecuteComboboxSelectionChanged(someObject);                        
}

public void InitializeReorderModel(string id)
{
    //set the MODEL
    this.Nachbest = new Nachbestellung();

    //Retrieve and set some values on *VIEW LOAD*!
    var dbOracle = new Datenbank();
    this.Nachbest.Bv = dbOracle.GetBauvorhaben(hv);
    this.Nachbest.Hv = hv;
    this.Nachbest.Bauleiter = dbOracle.GetBauleiter(hv);
    this.Nachbest.Projektleiter = dbOracle.GetProjektleiter(hv);
}

private void ExecuteComboboxSelectionChanged(object param)
{
    Empf = new ObservableCollection<string>()
    {
        "CompanyA","CompanyB","CompanyC"             
    };
Nachbest.Empf_Ansprechpartner = "CompanyB";
            Nachbest.Empfaenger_Mail = "orders@companyB.com";
            Nachbest.Empfaenger = SelValue; //if this is left out and there is no selection (just the default remaining unchanged!),Nachbest.Empfaenger will be null!
}
}
本文链接:https://www.f2er.com/3145770.html

大家都在问