I'm working on a C#/WPF application.
I'm invoking ProcessServiceResponse() method in MainViewModel on click of a button.
SelectedCountry property value is correctly getting set in this method.The country list combobox is also showing the list of countries.
But somehow, am not seeing a selected value(for e.g. SG) in the country dropdown list.
Any ideas as to what am I missing here please?
Let me know if you need any other details around the code.
Thanks.
Here's my 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;
}
}
}
I'm invoking ProcessServiceResponse() method in MainViewModel on click of a button.
SelectedCountry property value is correctly getting set in this method.The country list combobox is also showing the list of countries.
But somehow, am not seeing a selected value(for e.g. SG) in the country dropdown list.
Any ideas as to what am I missing here please?
Let me know if you need any other details around the code.
Thanks.
Here's my 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;
}
}
}