2016-09-30 4 views
0

Ich arbeite an einer C#/WPF-Anwendung. Ich Aufruf ProcessServiceResponse() -Methode in MainViewModel auf einen Klick auf eine Schaltfläche. Der Wert für SelectedCountry-Eigenschaft wird in dieser Methode korrekt festgelegt. Die Combobox Länderliste zeigt auch die Länderliste an. Aber irgendwie sehe ich keinen ausgewählten Wert (z. B. SG) in der Länder-Dropdown-Liste. Irgendwelche Ideen, was vermisse ich hier bitte? Lassen Sie mich wissen, wenn Sie weitere Details zum Code benötigen.Nicht in der Lage, den ausgewählten Wert in der Combobox zu sehen

Danke.

Hier ist mein Code.

MainWindow View: 

<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:MainViewModel="clr-namespace:MyTool.ViewModels" 
     xmlns:ViewModel="clr-namespace:MyTool.ViewModel.Bonds" 
     xmlns:View="clr-namespace:MyTool" x:Class="MyTool.MainWindow" 
     Title="{Binding DisplayName, Mode=OneWay}" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Height="600" Width="1100"> 
    <Window.DataContext> 
     <MainViewModel:MainWindowViewModel/> 
    </Window.DataContext> 


<ComboBox Margin="1,0" ItemsSource="{Binding MyViewModel.CountryList,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Description" SelectedValuePath="Code" SelectedItem="{Binding MyViewModel.SelectedCountry, Mode=TwoWay}" TabIndex="2" Grid.Row="7" Grid.Column="2" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="180" /> 

MainViewModel: 

public MainWindowViewModel() 
{ 
    MyAttributes = new MyViewModel(); 

} 

public object MyAttributes 
     { 
      get { return m_myViewModel; } 
      set 
      { 
       m_myViewModel = value; 
       OnPropertyChanged("MyAttributes"); 
      } 
     } 

public void ProcessServiceResponse() 

{ 

     var destination = new MyViewModel();/ 
      Type destinationType = destination.GetType(); 

      PropertyInfo[] destinationTypePI = destinationType.GetProperties(); 

      string propertyName = string.Empty; 
      object propertyValue = null; 

      foreach (var pinfo in sourcePI) 
      { 
       propertyName = pinfo.Name.Trim(); 
       var matchingItem = destinationTypePI.ToList().Where(d => d.Name == propertyName); 
       if (matchingItem != null && matchingItem.Count() > 0) 
       { 
        propertyValue = pinfo.GetValue(serviceResponse.lst_DKSecurities[0]); 
        matchingItem.FirstOrDefault().SetValue(destination, propertyValue);      
       } 

      } 

      this.MyAttributes = destination; 

} 

MyViewModel: 

namespace MyTool.ViewModels; 
public class MyViewModel 
{ 

public MyViewModel 
{ 
this.CountryList = GetCountryList(); 
} 

public string SelectedCountry 
     { 
      get 
      { 
       return m_selectedCountry; 
      } 
      set 
      { 
       m_selectedCountry = value; 
      } 
     } 

} 

Antwort

0

Nachdem Sie die Country füllen, weisen auf die SelectedCountry den Listeneintrag Sie anzeigen möchten:

CountryList = new ObservableCollection<string> {"A", "B", "C"}; 
     SelectedCountry = CountryList[0]; 

<ComboBox ItemsSource="{Binding CountryList, UpdateSourceTrigger=PropertyChanged}"   
       SelectedItem="{Binding SelectedCountry, Mode=TwoWay}" /> 
+0

Dank Rom für Ihre inputs.But Angst, dies wird nicht funktionieren wie die Liste Elementwert, dass ich angezeigt werden soll, steht nur in der ProcessServiceResponse() - Methode in meinem MainViewModel zur Verfügung. Und ich hole die Länderliste im Konstruktor von MyViewModel, die aufgerufen wird, bevor die ProcessServiceResponse() -Methode aufgerufen wird. –

Verwandte Themen