更改列表框SelectedValuePath抛出一个异常抛出、异常、列表、SelectedValuePath

2023-09-06 15:10:26 作者:伴她闹i

我写了一个快速应用以$ C $的CProject的文章(全文的此处您可以直接从的这里)。

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

这是一个非常简单的窗口,具有简单对象列表框(3个属性:2串1 INT)。

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; }
}

我设置了​​ SelectedValuePath 在code的后面,以便用户可以选择从组合框的属性,看看当前的的SelectedValue 的标签。

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; }
}

这两个属性是字符串,等等,应该是有效的输入的 SelectedValuePath

我设置的值是这样的:

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;
    }

(如果你下载的code,这是行79对MainWindow.xaml.cs)。

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

当我改变从字符串选择的值到 INT 的异常情况。 为了避免混淆,两者都将是字符串重属性的presentation显示。

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.

要重现错误,注释掉线7​​9运行演示:

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

选择任何的SelectedValuePath组合框(注:第二个)的第2串属性。 更改选择的列表框(你应该看到的SelectedValue 相应变化) 更改选择在第二个组合框的整型属性(这是一个字符串,再presentation真的)。 抛出一个异常与错误:输入字符串的不正确的格式 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"

奇怪的是:如果你重复的步骤,但是首先选择整型属性,它工作正常。更改为一个字符串,然后,仍然能正常工作。回到INT,抛出异常。

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.

设置 SelectedValuePath 来空设置似乎解决这​​一问题之前。任何建议,为什么你的异常被抛出,什么是问题?

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?

修改: 这里有一个新的演示与更多的属性类型。它也将显示哪些错误被抛出,并跟踪的SelectedIndex :下载新的演示。

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.

推荐答案

看起来item_index是不确定的,如果行78注释掉。 修改哎呀!被认为是行79。

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

编辑:确定,问题是,SelectedValuePath只是期待一个字符串转换为字符串时,你告诉它期待一个字符串。但是,当你告诉它,它的价值应该是一个整数,它希望你通过某种整数的字符串版本。然后,它可以转换成相应的整数的价值这一点。你还在传递一个名称,它无法转换为整数,并告诉你。如果通过code步骤和改变,而不是ShapeSides名称类似于2,它会正常运行。我建议检查的价值选择的指标,并设置名称= SelectedValue.ToString()如果类型应该是一个字符串或名称= SelectedIndex.ToString(),如果它应该是一个整数。

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;
    }