Changing ListBox SelectedValuePath throws an exception

落花浮王杯 提交于 2020-01-05 07:54:43

问题


I wrote a quick application for an article on CodeProject (full article here. You can download the source code for this question directly from here).

It's a very simple window that has a ListBox with simple objects (3 properties: 2 strings, 1 int).

public class MyShape
{
    public string ShapeType  { get; set; }
    public string ShapeColor { get; set; }
    public int    ShapeSides { get; set; }
}

I'm setting the SelectedValuePath in the code behind so a user can select a property from a combobox, and see the current SelectedValue in a Label.

The ComboBox is set to an object of type:

public class PropertyObject
{
    public string PropertyName { get; set; }
    public string PropertyType { get; set; }
}

Both properties are strings, and so, should be valid as input for the SelectedValuePath.

I'm setting the value like this:

private void ShapeClassPropertiesCmbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ComboBox cmbx = (ComboBox)sender;
        PropertyObject prop_ob = ((PropertyObject)cmbx.SelectedItem);
        string name = prop_ob.PropertyName;
        var item_index = SourceListBox.SelectedIndex;
        //SourceListBox.SelectedValuePath = null;       // without this, we get a null exceptions when going from string to int properties for some reason.
        SourceListBox.SelectedValuePath = name;
        SourceListBox.SelectedIndex = item_index;
    }

(If you download the code, it's line 79 on the MainWindow.xaml.cs).

The exception happens when I change the selected value from a string to an int. To avoid confusion, both will be string representation of the property to show.

To reproduce the error, comment out line 79. Run the demo:

  • Select any of the first 2 string properties on the SelectedValuePath combobox (2nd one).
  • Change the selection on the listbox (you should see the SelectedValue changing accordingly)
  • Change the selection in the 2nd combobox to the int property (which is a string representation really). An exception is thrown with the error: "Input string was not in a correct format"

The weird thing is: if you repeat the steps, but select the int property first, it works fine. Changing to a string then, still works fine. Back to the int, exception is thrown.

Setting the SelectedValuePath to null before setting it seems to solve the issue. Any suggestions why thy exceptions is thrown and what's the issue?

Edit: Here's a new demo with more property types. It will also show what error is thrown, and track the SelectedIndex: Download new demo.


回答1:


Looks like item_index is undefined if line 78 is commented out. Edit: Ooops! Was supposed to be line 79.

Edit: Ok, the problem is that SelectedValuePath is simply expecting to convert a string to a string when you tell it expect a string. But when you tell it that it that it's 'value' is supposed to be an integer, it expects you to pass in a string version of some kind of integer. It can then convert this into the appropriate integer value. Your still passing in a name, and it can't convert that to an integer and tells you so. If you step through the code and change name to something like "2", instead of "ShapeSides", it will run correctly. I recommend checking for the index of the value chosen, and setting name = SelectedValue.ToString() if the type is supposed to be a string or name = SelectedIndex.ToString() if it's supposed to be an integer.

    switch (SourceListBox.SelectedIndex)
    {
        case 0:
        case 1:
            name = SourceListBox.SelectedValue.ToString();
            break;
        case 2:
            name = SourceListBox.SelectedIndex.ToString();
            break;
    }


来源:https://stackoverflow.com/questions/19510426/changing-listbox-selectedvaluepath-throws-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!