Get SelectedItem of ComboBox Bound Multi-Colum List - c#

I have a multi-column List bound to a ComboBox.
It displays List Column Name as the ItemSource.
Problem
SelectedItem does return the Name.
With MessageBox.Show(vm.cboVideoQuality_SelectedItem); the string shows as MyProgram.ViewModel+VideoQuality instead of High.
I tried filtering Name with:
vm.cboVideoQuality_SelectedItem.FirstOrDefault(s => s.Name);
But it gives error 'char' does not contain definition for Name.
XAML
Bind Item Source and Selected Item.
<ComboBox x:Name="cboVideoQuality"
ItemsSource="{Binding cboVideoQuality_Items, Mode=TwoWay}"
DisplayMemberPath="Name"
SelectedItem="{Binding cboVideoQuality_SelectedItem, Mode=TwoWay}"
SelectedValuePath="Name"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="0,0,0,0"
Width="100" />
ViewModel
// Item Source
//
public class VideoQuality
{
public string Name { get; set; }
public string Bitrate { get; set; }
}
public List<VideoQuality> _cboVideoQuality_Items = new List<VideoQuality>()
{
new ViewModel.VideoQuality() { Name = "High", Bitrate = "5000K" },
new ViewModel.VideoQuality() { Name = "Medium", Bitrate = "2500K" },
new ViewModel.VideoQuality() { Name = "Low", Bitrate = "500K" },
};
public List<VideoQuality> cboVideoQuality_Items
{
get { return _cboVideoQuality_Items; }
set
{
_cboVideoQuality_Items = value;
OnPropertyChanged("cboVideoQuality_Items");
}
}
// Selected Item
//
public string _cboVideoQuality_SelectedItem { get; set; }
public string cboVideoQuality_SelectedItem
{
get { return _cboVideoQuality_SelectedItem; }
set
{
if (_cboVideoQuality_SelectedItem == value)
{
return;
}
_cboVideoQuality_SelectedItem = value;
OnPropertyChanged("cboVideoQuality_SelectedItem");
}
}
C#
I cannot check if selected = "High" because SelectedItem does not equal Name.
It equals MyProgram.ViewModel+VideoQuality.
ViewModel vm = mainwindow.DataContext as ViewModel;
string selected = vm.cboVideoQuality_SelectedItem;
if (selected == "High")
{
// ...
}

You have already used SelectedValuePath and should of course also use SelectedValue instead of SelectedItem:
<ComboBox
ItemsSource="{Binding cboVideoQuality_Items}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding cboVideoQuality_SelectedItem}" .../>
As a note, in both Bindings Mode=TwoWay is redundant. An ItemsSource binding is never two-way, while a SelectedValue binding is two-way by default.
I'd also recommend not to use binding target property details in view model property names. E.g. cboVideoQuality_SelectedItem should just be SelectedVideoQuality. Thus you may also bind it to the SelectedValue property of a ListBox or even the Text property of a TextBlock without too much confusion.

Related

search value in listview itemsource by index number wpf mvvm

I'm using Wpf MVVM, if i know the index number of item/row then how can i search the value in listview/itemsource by specific index number.
Note: i can get index number, index number will already be known.
below is the xaml code for listview
<ListView
Grid.Row="1"
ItemContainerStyle="{StaticResource FileItemStyle}"
ItemsSource="{Binding BarCode, IsAsync=True}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedIndex="{Binding SelectedIndex}"
SelectedItem="{Binding SelectBarCode,
UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single"
Style="{StaticResource ListItemsMain}"
and ObservableCollection for itemsource
private ObservableCollection<BarCodeModel> mBarCode = null;
public ObservableCollection<BarCodeModel> BarCode
{
get
{
mBarCode = mBarCode ?? new ObservableCollection<BarCodeModel>();
return mBarCode;
}
}
and below code is for model
public class BarCodeModel
{
public int BarCodeEntry_ID { get; set; }
public string BarCodeEntry_Title { get; set; }
and below is the command where i want to put my logic
private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
get
{
if (mSearchValueByIndexNumberCommand == null)
{
mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
{
// search BarCodeEntry_ID in BarCode where SelectedIndex is 5 (or other value)
});
}
return mSearchValueByIndexNumberCommand;
}
}
As in your XAML you bind your ListView.ItemsSource to BarCode of your ViewModel, but also bind ListView.SelectedItem and ListView.SelectedIndex to SelectBarCode and SelectedIndex, now, when you select some Item in a ListView, it (ListView) will update values of SelectBarCode and SelectedIndex in your ViewModel.
So, you can access your current selection with SelectBarCode or BarCode[SelectedIndex].
Below is answer to my question, a special thanks to #vasily.sib
private ICommand mSearchValueByIndexNumberCommand;
public ICommand SearchValueByIndexNumberCommand
{
get
{
if (mSearchValueByIndexNumberCommand == null)
{
mSearchValueByIndexNumberCommand = new DelegateCommand(delegate ()
{
int BarCodeId = BarCode[SelectedIndex].BarCodeEntry_ID;
});
}
return mSearchValueByIndexNumberCommand;
}
}

WPF ComboBox Selected Item with reference to an object

I'm trying to define a selected item in a combobox. It working fine if I'm just using a String to declare the selected item but not if using an object.
<ComboBox HorizontalAlignment="Left"
VerticalAlignment="Top" Width="81" materialDesign:HintAssist.Hint="Woche" Margin="10"
ItemsSource="{Binding weekSelection}"
DisplayMemberPath="name"
SelectedItem="{Binding nodeWeek, Mode=TwoWay}"
SelectedValue="name" />
-
private week _nodeWeek;
public week nodeWeek
{
get
{
return _nodeWeek;
}
set
{
_nodeWeek = value;
RaisePropertyChanged("nodeWeek");
}
}
-
public class week
{
public int val { get; set; }
public String name { get; set; }
}
-
setting the selected item
this.nodeWeek = new week() { val = times.GetIso8601WeekOfYear(DateTime.Now), name = "KW " + times.GetIso8601WeekOfYear(DateTime.Now).ToString() };
Is there a way to fix that?
The selected item must be always one of the list of your items source. You cannot create new objects and assign them to the SelectedItem. The Combobox simply compares object references not the content.

Add combobox text to its Itemssource

I have a combobox binded to a table called Tenderness through MVVM. I'm using Entity Framework.It displays all the records properly, but I need to add another functionality to it. Suppose the user types in text which is not contained inside the combobox's Itemssource, I want to be able to add it directly to the table and then update the Itemssource as well. Now I have been able to do this without MVVM, I would want to know, how to achieve it using MVVM.
Just do what you did previously in the LostFocus event handler in the setter of a source property that you bind to the Text property of the ComboBox.
View Model:
public ObservableCollection<string> Items { get; } = new ObservableCollection<string>() { "a", "b", "c" };
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged(nameof(Text));
//add the missing value...
if (!Items.Contains(_text))
Items.Add(_text);
}
}
private string _selectedItem;
public string SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged(nameof(SelectedItem));
}
}
View:
<ComboBox IsEditable="True" Text="{Binding Text, UpdateSourceTrigger=LostFocus}" ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}" />

DropDown bind to property names; based on first set second DropDown binding

I have one ObservableCollection<M> fooBar {get;set;}. The class M.cs looks like this:
public class M{
private int _ID;
public int ID {
get {return this._ID;}
set {this._ID = value;}
}
private string _number;
public int Number {
get {return this._number;}
set {this._number = value;}
}
private string _power;
public int Power {
get {return this._power;}
set {this._power = value;}
}
/*
...
*/
}
Now I want to bind the names of these 3 propertys to a ComboBox. I don't want to do it like this:
<ComboBox>
<ComboBoxItem>ID</ComboBoxItem>
<ComboBoxItem>Number</ComboBoxItem>
<ComboBoxItem>Power</ComboBoxItem>
</ComboBox>
Is there a more comfortable way?
Based on the choose of the first ComboBox I want to fill the second ComboBox. As example I choose in the first ComboBox the property Numberthen the second ComboBox should look like this
<ComboBox
SelectedValue="{Binding ???}"
ItemsSource="{Binding fooBar}"
SelectedValuePath="Number"
DisplayMemberPath="Number"
/>
Maybe someone of you can help me, because I have no idea how to connect both comboboxes.
This is how I would do it:
Make a property on the view model which exposes the properties on the model (class M) which can be selected. This way you explicitly control which properties can be selected.
Make a property to hold the selected value of each combo box.
DisplayMemberPath/SelectedValuePath in ComboBox2 binds to the SelectedValue of ComboBox1.
ViewModel:
// ComboBox 1
public Dictionary<string, string> SelectableProperties = new Dictionary<string, string>()
{
{ nameof (M.ID), "ID" }
{ nameof (M.Number), "Nummer" }
{ nameof (M.Power), "Potenz" }
}
// Selection in combobox 1 (not strictly necessary as it can be handled in view, but you may need to know what SelectedValue represents)
private string _selectedValueMember = String.Empty;
public string SelectedValueMember
{
get { return _selectedValueMember; }
set { _selectedValueMember = value; }
}
// Selection in combobox 2 (object just in case there could be other values than int)
private object _selectedValue = null;
public object SelectedValue
{
get { return _selectedValue; }
set { _selectedValue = value; }
}
public ObservableCollection<M> FooBar{ get; set; }
View:
<ComboBox x:Name="ComboBox1"
Width="100"
Margin="5"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding SelectedValueMember}"
ItemsSource="{Binding SelectableProperties}">
</ComboBox>
<ComboBox Width="100"
Margin="5"
DisplayMemberPath="{Binding ElementName=ComboBox1, Path=SelectedValue}"
SelectedValuePath="{Binding ElementName=ComboBox1, Path=SelectedValue}"
SelectedValue="{Binding SelectedValue}"
ItemsSource="{Binding FooBar}">
</ComboBox>
For the 1st ComboBox: Use Reflection to get the names of all the properties of class M and put those into the ComboBox.
For the 2nd ComboBox: When selection changes in the 1st one, you get the property name. Now set the SelectedValue binding to the property that was selected in the 1st ComboBox.

Combo box binding in mvvm

I am not able to find out Why is combo box binding not working?
I have a view model which looks like (2 properties)
public ProcessMaintenanceDataObject CurrentProcess
{
get
{
return _CurrentProcess;
}
set
{
_CurrentProcess = value;
OnPropertyChanged("CurrentProcess");
}
}
public ObservableCollection<ProcessMaintenanceDataObject > Processes
{
get
{
return _Processes;
}
set
{
_Processes = value;
OnPropertyChanged("Processes");
}
}
public ObservableCollection<FolderInfo> Folders
{
get
{
return _folders;
}
set
{
_folders = value;
OnPropertyChanged("Folders");
}
}
The following is the ProcessMaintenanceDataObject definition
[DataMember]
public string ProcessName
{
get
{
return _ProcessName;
}
set
{
_ProcessName = value;
OnPropertyChanged("ProcessName");
}
}
[DataMember]
public string Id
{
get
{
return _Id;
}
set
{
_Id = value;
OnPropertyChanged("Id");
}
}
[DataMember]
public string FolderId
{
get
{
return _FolderId;
}
set
{
_FolderId = value;
OnPropertyChanged("FolderId");
}
}
[DataMember]
public FolderInfo Folder
{
get
{
return _Folder;
}
set
{
_Folder = value;
if (_Folder != null)
FolderId = _Folder.FolderId;
OnPropertyChanged("Folder");
}
}
The FolderInfo class has FolderName and FolderId Property.
I have a method in viewmodel which fills the Processes.
In my view I have structure something like, I have a treeview which will be bound to Processes and while selecting any of the item from the treeview, i need to allow user to edit that entity.
In the view the combo box binding is as:
<ComboBox ItemsSource="{Binding Path=Folders, Mode=OneWay}"
DisplayMemberPath="FolderName"
SelectedItem="{Binding Source={StaticResource viewModel}, Path=CurrentProcess.Folder, Mode=TwoWay}">
...
This binding doesn't work I mean when I select any object from the tree it fills other information like ProcesName in the textBox but it doesn't make the Folder object as the selected item in combobox, however the combo box will be filled.
Any suggestion.
Do refer this:
If you want to bind a ComboBox to your folders property in two way mode with edit support,
Then you should define data template for your combo box and then bind properties of FolderInfo class to those text boxes
Binding display member path will not solve your problem
I'd suggest you to change DisplayMemberPath with appropriate DataTemplate:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding FolderName}">
</StackPanel>
<DataTemplate>
This will fix SelectedItem context.
Maybe just maybe, your Folderinfo class is not a notificationObject. If it is the case make sure it implements INotifyPropertyChange.
you have to use SelectedValuePath and SelectedValue instead of SelectedItem like below,
<ComboBox ItemsSource="{Binding Path=Folders, Mode=OneWay}"
DisplayMemberPath="FolderName"
SelectedValuePath="FolderId"
SelectedValue="{Binding Path=FolderId, Mode=TwoWay}">
SelectedItem binds the whole object whereas SelectedValue binds only particular properties of the object.

Categories

Resources