0
votes

in my ListBox , I have 5 numbers 1 - 5 , SelectedItem is databind in TwoWay Mode to SelectedAmount , an int property which has a default value of 1 , whenever I run the Application I have 1 as value selected and highlighted on my ListBox, thats very good but now whenever I change the value to 2 or any other value , the ListBox shows the new value but it is not highlighted , and the SelectedAmount is still holding the old value , that is obviously bad , I want whenever the user changes the selected value, SelectedAmount to get the new value directly , the user now after he made his selection,had to click again to highlight it. any ideas ?

<ListBox ItemsSource="{Binding SiliconAmounts}" SelectedItem="{Binding SiliconAmount,Mode=TwoWay}" Height="31" HorizontalAlignment="Left" Margin="83,148,0,0" VerticalAlignment="Top" Width="149" IsTabStop="True" TabIndex="4" />

my property databind to selectedItem looks like

public int SiliconAmount
    {
        get
        {
            return _SiliconAmount;
        }
        set
        {
            if (_SiliconAmount != value)
            {
                _SiliconAmount =value;
                OnPropertyChanged("SiliconAmount");
            }
        }
    }

my items property looks like

public ObservableCollection<int> SiliconAmounts
    {
        get { return _SiliconAmounts; }
        set
        {
            if (_SiliconAmounts != value)
            {
                _SiliconAmounts = value;
                OnPropertyChanged("SiliconAmounts");
            }
        }
    }

on my viewModel constructor

SiliconAmounts = new ObservableCollection<int>();
        SiliconAmounts.Add(1);
        SiliconAmounts.Add(2);
        SiliconAmounts.Add(3);
        SiliconAmounts.Add(4);
        SiliconAmounts.Add(5);

thanks in advance

1
can you show code to reproduce this?stijn
sure, I will edit the post to include codeMusaab
I have no idea if this works but try SelectedValue instead of SelectedItem.Ingó Vals
I did, the same problemMusaab
Check the Output pane in VS for any binding errors. Are all of the bindings set up properly? How are you establishing the data context of the ListBox? All of the code that you show here looks good.Josh G

1 Answers

0
votes

After posting and reading the comments, it sounds like the problem is UI styling. I have found that Expression Blend is a helpful tool for writing styles and templates on controls. I tend to like to code direct XAML quite a bit, but Expression has an editor for this and some nice tools for setting up visual states, visual triggers, selecting colors, and more. You can get a trial of Blend here. Another tool a lot of WPF developers use is Kaxaml.