I create an WPF app in VS 2015 express environment. Right now I'm struggling with combobox value presentation problem. My combobox looks like that:
<ComboBox
x:Name="cb_pers_ucz"
ItemsSource="{Binding Path=Mechanizmy.GlobalObj.SLO_PER_UCZ}"
DisplayMemberPath="nazwa"
SelectedValuePath="id_poz"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Mechanizmy.GlobalObj.SLO_PER_UCZ, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ToolTip=""
HorizontalAlignment="Left"
Margin="175,127,0,0"
VerticalAlignment="Top"
Width="185"
Height="22"
Grid.Row="1"
ToolTipOpening="cb_pers_ucz_ToolTipOpening"
/>
Everything works fine till I need to set selected value via c# code. When I use SelectedIndex or SelectedValue property combobox is changing ID value to selected but there's blank value on window (GUI).
I'm using List "SLO_PER_UCZ" to fill combobox. It looks like that:
public class slowniki
{
public int id_poz { get; set; }
public string nazwa { get; set; }
public string definicja { get; set; }
public DateTime dt_od { get; set; }
public DateTime dt_do { get; set; }
}
public static List<slowniki> SLO_PER_UCZ = new List<slowniki>();
Sorry for quality of Combobox structure, but I was trying to implement several solutions that I found on stackoverflow and other portals (with no effect).
Mechanizmy.GlobalObj.SLO_PER_UCZ
is the collection. It can't be the selected value as well. BindSelectedValue
to a different property that will take the selected value.Since you want to set it as well, I'm also showing you how to implement
INotifyPropertyChanged
on your viewmodel.XAML:
And don't use
UpdateSourceTrigger=PropertyChanged
orMode=TwoWay
until you find out what they mean. Adding random stuff you don't understand just wastes your time and gets you nowhere.