根据ListBox中的SelectedItem更改Dictionary的值

“我的文本框”显示一个类的属性值,该类的值根据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" VerticalAlignment="Top" Width="288" SelectionChanged="listBox_SelectionChanged"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="523,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; }
    }
}

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

kobechf301 回答:根据ListBox中的SelectedItem更改Dictionary的值

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

只需将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) }
    };
}
本文链接:https://www.f2er.com/3116426.html

大家都在问