WPF - combobox c# selection is not displayed on active window

110 Views Asked by At

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).

2

There are 2 best solutions below

0
On BEST ANSWER

Mechanizmy.GlobalObj.SLO_PER_UCZ is the collection. It can't be the selected value as well. Bind SelectedValue 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.

public class GlobalObjClassName : INotifyPropertyChanged
{

    //  ... etc. etc. etc. ...
    public static List<slowniki> SLO_PER_UCZ = new List<slowniki>();

    private int _selectedIDPoz = -1;
    public int SelectedIDPoz
    {
        get { return _selectedIDPoz; }
        set
        {
            if (value != _selectedIDPoz)
            {
                _selectedIDPoz = value;
                OnPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] String propName = null)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));

XAML:

<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.SelectedIDPoz}" 
    ToolTip="" 
    HorizontalAlignment="Left" 
    Margin="175,127,0,0" 
    VerticalAlignment="Top" 
    Width="185" 
    Height="22" 
    Grid.Row="1" 
    ToolTipOpening="cb_pers_ucz_ToolTipOpening"
    />

And don't use UpdateSourceTrigger=PropertyChanged or Mode=TwoWay until you find out what they mean. Adding random stuff you don't understand just wastes your time and gets you nowhere.

0
On

Thanks a lot for Your answer. It wasn't strick answer to My question but gave me enough information to solve problem.

Once more thanks :)

Kind Regards Sebastian