根据ListBox中的SelectedItem改变Dictionary的值

Change the value of Dictionary based on the SelectedItem in ListBox

My TextBox 显示 属性 的 class 值,该值是根据 ListBox 中的 SelectedItem 更改的。 (到目前为止,一切顺利。)但是,现在我想用用户指定的值替换 Dictionary 中定义的值(key 是 ListBox 中的 SelectedItem)。这不管用,不管我做什么。它只是抛出一个异常。这是我的完整代码:

MainWindow.xaml.cs

using ChangeTextBoxBasedOnListBox.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

public partial class MainWindow : Window
    {
        ObservableCollection<string> oc;
        ObservableCollection<Graph> graph;
        Dictionary<string, Graph> dic = new Dictionary<string, Graph>();
        ListBox lB;

        public MainWindow()
        {
            InitializeComponent();

            oc = new ObservableCollection<string>()
            {
                "Test_1",
                "Test_2"
            };

            graph = new ObservableCollection<Graph>()
            {
                new Graph(10),
                new Graph(100)
            };

            listBox.ItemsSource = oc;

            foreach (var test in oc.Select((k, i) => new { kvp = k, index = i }))
            {
                dic.Add(test.kvp, graph[test.index]);
            }
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Graph dicValue = dic[(sender as ListBox).SelectedItem.ToString()];
            textBox.Text = Convert.ToString(dicValue.Step);
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            // This works, but this isn't what I want to do
            if (dic.ContainsKey("Test_1"))
                dic["Test_1"].Step = 1000;

            // What I want to do is:
            // (dic[(The current SelectedItem in ListBox)].Step  = (The value user specified)

            // This doesn't work ... throws an exception
            //if (lB.SelectedItem != null)
            //{
            //    if (dic.ContainsKey(lB.SelectedItem.ToString()))
            //    {
            //        dic[lB.SelectedItem.ToString()].Step = (sender as Graph).Step;
            //    }
            //}
        }
    }
}

MainWindow.xaml

<Window x:Class="ChangeTextBoxBasedOnListBox.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:ChangeTextBoxBasedOnListBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="238" Margin="88,82,0,0" VerticalAlignment="Top" Width="288" SelectionChanged="listBox_SelectionChanged"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="523,82,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>

    </Grid>
</Window>

Model/Graph.cs

namespace ChangeTextBoxBasedOnListBox.Model
{
    public class Graph
    {
        public Graph(int step) { Step = step; }

        public int Step { get; set; }
    }
}

...我想我只需要再走一步,但我找不到路。请帮我。提前谢谢你。

你的方法太复杂了。实际上,您根本不需要任何事件处理程序。

只需将 Dictionary 直接传递给 ListBox 的 ItemsSource,然后将 DisplayMemberPathSelectedValuePath 设置为其 KeyValue 属性。因此 ListBox 显示键字符串,您可以通过 ListBox 的 SelectedValue 属性 直接访问 Graph 实例。

<StackPanel>
    <ListBox x:Name="listBox" DisplayMemberPath="Key" SelectedValuePath="Value"/>
    <TextBox Text="{Binding SelectedValue.Step, ElementName=listBox}"/>
</StackPanel>

一切都自动运行,后面有这段代码:

public MainWindow()
{
    InitializeComponent();

    listBox.ItemsSource = new Dictionary<string, Graph>()
    {
        { "Test_1", new Graph(10) },
        { "Test_2", new Graph(100) }
    };
}