Hi,
The basic of binding this would be using a list to create the data in view model. I will try to explain it here.
1.) Create a sample class :
public class ComboData : INotifyPropertyChanged
{
private string _text { get; set; }
private string _values { get; set; }
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
public string Values
{
get { return _values; }
set
{
_values = value;
OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2.) In your class add the properties for two lists:
List<ComboData> _typeList = new List<ComboData>();
public List<ComboData> TypeList
{
get { return _typeList; }
set
{
_typeList = value;
RaisePropertyChanged(() => TypeList);
}
}
List<ComboData> _statusList = new List<ComboData>();
public List<ComboData> StatusList
{
get { return _statusList; }
set
{
_statusList = value;
RaisePropertyChanged(() => StatusList);
}
}
These two properties are in my view model. If you are wondering what is RaisePropertyChanged() here, it is the implementation for my ViewModelBase.
My sample ViewModelBase can be like:
public abstract class ViewModelBase : INotifyPropertyChanged
{
protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
{
var propertyName = GetPropertyName(action);
RaisePropertyChanged(propertyName);
}
private static string GetPropertyName<T>(Expression<Func<T>> action)
{
var expression = (MemberExpression)action.Body;
var propertyName = expression.Member.Name;
return propertyName;
}
public void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
3.) Create a sample data in your view model constructor. If you are not using mvvm approach, create this in your Loaded event:
TypeList.Add(new ComboData {Text = "1234", Values = "1234"});
TypeList.Add(new ComboData {Text = "1234", Values = "41234"});
StatusList.Add(new ComboData {Text = "agfasd", Values = "asgas"});
StatusList.Add(new ComboData {Text = "ag", Values = "asdg"});
So basically, here we have a text property that will be for display binded with a value for its value member path.
4.) Add the ComboBox in your xaml:
<combobox grid.row="1" grid.column="1" x:name="CbType" itemssource="{Binding TypeList}" displaymemberpath="Text" selectedvaluepath=" Values" xmlns:x="#unknown" />
This should work.
Try searching on google and see what are basics of binding a combobox.
Hope this helps !