Hello,
Can someone tell me what the difference is and when to take what?

Hello,
Can someone tell me what the difference is and when to take what?

Hi Markus,
iit's easy to bind SelectedItem an in Setter call your Processing. Try this demo:
<Window x:Class="WpfApp1.Window72"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp72"
mc:Ignorable="d"
Title="Window72" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<ComboBox Height="20" Width="100"
ItemsSource="{Binding ComList}"
DisplayMemberPath="CmbValue"
SelectedItem="{Binding SelectedCom}"/>
</Grid>
</Window>
And classes:

Hi Peter,
that looks really good and understandable. Thank you very much, I'll test it.
If you still want to selected some item when load the ComboBox, you can use SelectedIndex="" to implement it.SelectedIndex="4" and SelectedItem="{Binding Comlist[4]}" have the same effect.
FrameworkElement.DataContext property gets or sets the data context for an element when it participates in data binding.FrameworkElement.Resources Property gets or sets the locally-defined resource dictionary. They are both can set the datacontext for the FrameworkElement. If you use the Resources to host the data soruce, you need to set the DataContext for the element. In this demo, you can use it like below:
<Window.Resources>
<local:ViewModel x:Key="MyData"></local:ViewModel>
</Window.Resources>
<Grid>
<ComboBox Name="cbStatus"
DataContext ="{StaticResource MyData}"
ItemsSource="{Binding Comlist}"
SelectedItem="{Binding Comlist[4]}"
SelectedValuePath="CmbValue"
DisplayMemberPath="CmbText"
Width="100" Height="30"
SelectionChanged="cbStatus_SelectionChanged"
/>
</Grid>
Hello,
Thanks for quick response.
Am I seeing this right? See my comment.
And how do I get the selected data into the ViewModel correctly? According to MVVM.



The diference you said is right.If you want to use diferent data collections in the View.Why do not you set them in the same viewmodel? And could you tell me why do you want get the data from View in ViewModel?
OK, I understood the difference.
1- UserInterface
2- ViewModel, I use this
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _action;
public RelayCommand(Action<object> action) { _action = action; _canExecute = null; }
public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
public void Execute(object o) => _action(o);
public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
3-Model, here my special combo box
4 - MainWindow according to my research, there shouldn't be any code.
1 Person is following this question.