如何使用ICommand将组合框事件绑定到视图模型

问题描述 投票:0回答:1

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

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

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

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,0" x:Name="leftStPnl" DataContext="{Binding Nachbest}">
            <ComboBox x:Name="cboxEmpfaenger" 
                      ItemsSource="{Binding Empf}" 
                      Text="{Binding Empfaenger}" 
                      FontSize="12" Width="150" Margin="118,0,0,0"
                      SelectedIndex="12"
                      SelectedValue="{Binding SelValue}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <i:InvokeCommandAction Command="{Binding LoadCombobox}"/>
                    </i:EventTrigger>
                    <i:EventTrigger EventName="SelectionChanged">
                         <i:InvokeCommandAction Command="{Binding ComboboxSelectionChanged}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
            <TextBox x:Name="txtEmpfAnsprechpartner" Text="{Binding Empf_Ansprechpartner}" FontSize="12" IsEnabled="False" Width="150" Margin="50,0,0,0"/>    
            <TextBox x:Name="txtEmpfMail" Text="{Binding Empfaenger_Mail}" FontSize="12" IsEnabled="False" Width="150" Margin="73,0,0,0"/>
        </StackPanel>
    </Grid>
</Window>

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

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

视图模型(neueNachbestellungViewModel.cs)

public ICommand LoadCombobox => new DelegateCommand<object>(ExecuteLoadCombobox);
public ICommand ComboboxSelectionChanged => new DelegateCommand<object>(ExecuteComboboxSelectionChanged);
public Nachbestellung Nachbest { get; set; }
public ObservableCollection<string> Empf { get; set; }
public string SelValue { get; set; }

public neueNachbestellungViewModel(string id)
{

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

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 = "[email protected]";
}

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

    switch (SelValue)
    {

        case "CompanyA":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyA";
                Nachbest.Empfaenger_Mail = "[email protected]";
            }
            break;

        case "CompanyB":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyB";
                Nachbest.Empfaenger_Mail = "[email protected]";
            }
            break;

        case "CompanyC":
            {
                Nachbest.Empf_Ansprechpartner = "CompanyC";
                Nachbest.Empfaenger_Mail = "[email protected]";
            }
            break;

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

查看片段

enter image description here

c# wpf xaml mvvm icommand
1个回答
-2
投票
  1. [在您的代码中而不是您的母语中使用英语是一种很好的做法。
  2. 您需要创建ComboBoxItem并将其绑定到组合框的xaml'SelectedItem'属性。
  3. 对字符串进行硬编码是不好的做法,请尽可能使用资源文件。
  4. 您需要在viewModel的构造函数中初始化ObserableCollection。它也没有出现,因为当您的视图想要使用它时它不存在。
  5. 您为什么两次创建新的可观察项目集合?进行一次,然后使用您的class属性。
  6. 使用封装是一种好习惯。
  7. 代替视图中的事件,使用组合框的Command参数。
© www.soinside.com 2019 - 2024. All rights reserved.