【问题标题】:Update the index in a list<string>更新列表中的索引<string>
【发布时间】:2018-06-01 00:11:57
【问题描述】:

我有一个列表框,其中包含来自两个单独列表 NameListDescriptionList 的值的项目。

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

如果我删除顶部的一个字符串,所有字符串都会被索引弄乱并显示错误的值。可以更新list&lt;string&gt;的索引吗?

我尝试使用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。

但是,当我删除 1 然后选择图片中的 3 时,它将输出为 2

这是因为索引全部下降一。制作对象 3 -> 对象 2 等

我该如何解决这个问题??

【问题讨论】:

标签: c# wpf string list listbox


【解决方案1】:

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

  Label.Content = NameList[listBox.SelectedIndex];

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

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

【讨论】:

  • 我通过以下方式添加项目 --- main.listBox.Items.Add(string.Format("{0}: {1} ", name, now)); --- 在这种情况下,名称是字符串,现在是 DateTime.Now... 所以我不能使用 listbox.selecteditem.tostring();因为它会显示很多信息(时间)。有没有办法更新列表中的索引
  • 把事情搞清楚。当我使用 SelectedIndex 时,我可以按照我想要的方式显示值。仅来自 NameList 的字符串。但是一旦我删除了一个字符串,我就会在其他字符串中得到错误的索引。这就是为什么我想知道您是否可以使用 SelectedItem 或更新 list index
  • 你可以使用listBox.SelectedItem.Name ..这是你想要的吗?
  • @PanoKatsourakis 我没有任何选项可以使用listBox.SelectedItem.Name。我现在编辑了我的问题
【解决方案2】:

我建议你使用一些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 而无需修改其背后的逻辑。

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2021-05-01
    • 2010-11-22
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多