-1

I have a WPF ComboBox that binds to a set of data. I do not have permissions to modify the control directly, nor can I change the data.

I'm returned 1 item in my ComboBox, but there are actually 2 rows; the blank row and my expected value. Both appear to have an index value of 0. I don't want to see this blank row, just my expected data in the ComboBox auto-selected. I have looked through everyone's related posts in here, but none of the solutions have worked for my case. I have been programming for a long time, but still fairly new to WPF. Thanks for the help.

XAML

<MyComboBox Name="myTemplate5" MyLookup="Lookup" MyFilter="att_idn=-37" MyData="Detail" MyName="comp_tmpl_idn_srt" ModCde="31" MyEmptyValue="0" ToolTip="Have a nice day" Margin="0,2.5,30,2.5" MinWidth="120" Grid.Column="1"  SelectionChanged="myTemplate5_SelectionChanged" />

C#

private void myTemplate1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MyComboBox work = sender as MyComboBox;
    if (work != null && work.HasSelectionChanged(e))
    {
        int compTmplId = int.Parse(work.SelectedValue.ToString());
        if (!_wpfIsDumb && !ChangeComponent(compTmplId))
        {
            _wpfIsDumb = true;
            work.SelectedItem = e.RemovedItems[0];
            _wpfIsDumb = false;
        }
    }
}

public bool HasSelectionChanged(SelectionChangedEventArgs e)
{
    if (e.RemovedItems.Count > 0 && e.AddedItems.Count > 0)
        return true;
    else
        return false;
}
Yael
  • 1,566
  • 3
  • 18
  • 25
Andrew Day
  • 127
  • 2
  • 12
  • Thank you for the reply. Unfortunatley I cannot add new items to the combobox, because of binding permissions. The root cause is the function that checks if the combobox was changed. The RemovedItems.Count and the AddedItems.Count both need to be greater than zero in order to process as changed. I see 2 items though, one of which is blank (and is the selected item). It is not really there though, meaning the selected index is 0 regardless of the item I choose, so RemovedItems.Count is always 0, thus it thinks nothing was changed. – Andrew Day Jul 19 '12 at 20:05

2 Answers2

2

I found the solution. The selected index did not work. The problem was with the data. I was getting a NULL value passed to the box. Once I stripped out the NULL return from SQL, then it worked as expected.

Andrew Day
  • 127
  • 2
  • 12
1

You can achieved this by setting the SelectedIndex to 0.

XAML:

<ComboBox Name="myCB" 
                  SelectedIndex="0"
                  MaxWidth="200" MaxHeight="25" />

Code-behind:

namespace nsComboBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            myCB.Items.Add("Item 1");
            myCB.Items.Add("Item 2");
            myCB.Items.Add("Item 3");

            myCB.SelectedIndex = 0;
        }     
    }
}
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65