更新列表中的索引<string>

时间:2018-06-01 00:11:57

标签: c# wpf string list listbox

我有一个listBox,其中包含来自两个单独列表NameList&amp;的值的项目。 DescriptionList

当我选择一个项目时,它会在selectedIndex的项目中显示一些信息,如“名称”和“描述”。

如果我删除顶部的一个字符串,所有字符串将被索引搞乱并显示错误的值。你能更新list<string>的索引吗?

我尝试使用Label.Content = NameList[listBox.SelectedItem];,但它只是给出了参数1:无法从'object'转换为'int'。

修改1

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    List<string> CallNameList = Control.NameList;
    List<string> CallDescList = Control.DescriptionList;

    if (listBox.SelectedIndex < 0)
    {
        TextLabel.Text = "Please select another item";
    }
    else
    {
        TitelLabel.Content = CallNameList[listBox.SelectedIndex]; 
        TextLabel.Text = CallDescList[listBox.SelectedIndex]; 
    }
}

正如你在第一个场景中看到的,我得到的数字是1-2-3-4。我选择1时显示1。 Picture two

然而,当我删除1然后如图所示选择3时,它输出为2 Picture one

这是因为索引对所有人都下降了一个。制作对象3 - &gt;对象2等。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我很确定错误是您尝试使用SelectedItem(对象)而不是SelectedIndex(int)

  Label.Content = NameList[listBox.SelectedIndex];

或者你可以从SelectedItem中获取一个值

  Label.Content = listBox.SelectedItem.ToString();

答案 1 :(得分:0)

我建议你使用一些DataBinding。使用索引,并与它同步两个列表是....不太方便。

我们可以认为您有与您的约会相关的对象。 所以我创建了一个类:

public class CustomDate
{
    public string name { get; set; }
    public string description { get; set; }
    public CustomDate(string iName, string iDescription)
    {
        this.name = iName;
        this.description = iDescription;
    }
}

然后我使用ObservableCollection来处理这些对象。它与List类似,但会非常顺利地处理元素的添加/删除。

public partial class MainWindow : Window
{
    ObservableCollection<CustomDate> CallNameList = new ObservableCollection<CustomDate>();

    public MainWindow()
    {
        InitializeComponent();

        CallNameList.Add(new CustomDate("Date #1", "Description of first date"));
        CallNameList.Add(new CustomDate("Date #2", "This is a fantastic date"));
        CallNameList.Add(new CustomDate("Date #3", "It will rain today"));

        MyList.DataContext = CallNameList;
    }

    private void Delete_Click_1(object sender, RoutedEventArgs e)
    {
        CallNameList.Remove((CustomDate)MyList.SelectedItem);
    }
}

这是XAML / WPF部分。您应该关注DataBinding部分。如果你不熟悉它,你应该找到并阅读一些教程。没有DataBinding的WPF毫无意义。

<Window x:Class="StackTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:StackTest"
    mc:Ignorable="d"
    Title="Laser Control " Height="300" ResizeMode="CanMinimize" Cursor="Arrow" Width="400">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <ListBox Name="MyList" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Name="Delete" Click="Delete_Click_1" Grid.Row="1" Content="Delete current date"/>
    </Grid>
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" HorizontalAlignment="Center" Text="{Binding ElementName=MyList, Path=SelectedItem.name}"/>
        <TextBlock Grid.Row="1" HorizontalAlignment="Center" Text="{Binding ElementName=MyList, Path=SelectedItem.description}"/>
    </Grid>
</Grid>

</Window>

这种做法并不是唯一的,但我发现它非常方便,因为DataBinding正在为我处理所有事情。并且可以非常轻松地添加更多信息,或修改UI而无需修改其背后的逻辑。