使用按钮在Visual Studio 17中检索字典数据,C# [英] Using a button to retrieve dictionary data in Visual Studio 17, C#

查看:97
本文介绍了使用按钮在Visual Studio 17中检索字典数据,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景 - 使用Visual Studio 17,C# 

Background - using Visual Studio 17, C# 

我正在为工程项目设计用户界面控制器,我仍然是Visual Studio的新手。我有一个Pin to Pin连接列表,可以通过两个列表框(lsbPin1和lsbPin2)选择,一个按钮将选中的
引脚传输到第三个列表框(listBox1)。我有这个工作正常,我需要这个,以便用户知道他们已经建立了什么针脚连接。

I'm working on designing a user interface controller for an engineering project and am still very new to Visual Studio. I have a list of Pin to Pin connections that can be selected via two listboxes (lsbPin1 and lsbPin2) and a button transfers the selected pins to a 3rd listbox(listBox1). I have this working properly and I need this so that the user knows what pin connections they have made.

listBox1.Items.Add(selecteditem1 +" to" + selecteditem2);

按钮中的上述代码完全按照我的需要工作,它只是为了清晰起见而包含在列表框1中,其中包括"引脚1到引脚2"和"引脚2到引脚4"等。 。

The above code within the button works exactly as I need it to, it is just included for clarity and populates listBox1 with things like 'Pin 1 to Pin 2' and 'Pin 2 to Pin 4' etc...

我还创建了一个字典,其中每个引脚编号都分配了自己的十六进制值,以便接口可以与控制器通信,这两个引脚需要连接。接口将能够读取十六进制值并根据需要建立连接
,第四个列表框将保存此信息并将其发送给控制器。

I also have created a dictionary where each pin number has been assigned its own hex value so that the interface can communicate to the controller which two pins need to be connected. The interface will be able to read the hex values and make the connections as needed and the 4th listbox will hold this information and send it to the controller.

我需要什么帮助with使用相同的按钮填充第四个列表框(此示例中为listBox2),仅显示所选引脚的十六进制值。我怀疑代码看起来与我上面使用的代码非常相似,但是我找不到将
从字典中放置所选项目的十六进制值的语法 进入列表框。 ListBox2最终应该是'01F到02F'和'02F到04F',它们与listBox1中的项目直接相关。 

What I need help with is populating a 4th listbox (listBox2 for this example) with only the hex values of the selected pins - using the same button. I suspect the code will look very similar to what I'm using above but I can't find the syntax that would place the selected item's hex value from the dictionary  into the listbox. ListBox2 should end up with something like '01F to 02F' and '02F to 04F' which correlates directly with the items in listBox1. 

感谢您的帮助,时间和耐心。

Thank you for your help, time, and patience.

推荐答案

嗨Jeremy,你好b $ b看这个例子:

Hi Jeremy,
look this example:

XAML:

<Window x:Class="WpfApp1.Window16"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="Window16" Height="450" Width="800">
  <Window.DataContext>
    <local:Window16VM/>
  </Window.DataContext>
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0" 
             ItemsSource="{Binding View1}" 
             DisplayMemberPath="Description"
             SelectedItem="{Binding Selected1}" 
             Margin="5"/>
    <ListBox Grid.Column="1" 
             ItemsSource="{Binding View2}" 
             DisplayMemberPath="Description"
             SelectedItem="{Binding Selected2}" 
             Margin="5"/>
    <Button Grid.Column="2" Content="Copy Selected Items" Command="{Binding Cmd}" Height="50" Margin="5"/>
    <Grid Grid.Column="3">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <ListBox Grid.Row="0"
               ItemsSource="{Binding View3}" 
               DisplayMemberPath="ConnectedDescription"
               Margin="5"/>
      <ListBox Grid.Row="1"
               ItemsSource="{Binding View3}" 
               Margin="5">
        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="{Binding Number, StringFormat={}{0:x2}}"/>
              <TextBlock Text=" - "/>
              <TextBlock Text="{Binding ConnectedNumber, StringFormat={}{0:x2}}"/>
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </Grid>
  </Grid>
</Window>

使用System.Collections.ObjectModel的ViewModel:

And the ViewModel:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApp1
{
  public class Window16VM
  {
    public Window16VM()
    {
      for (byte i = 1; i <= 10; i++) col1.Add(new Pin() { Number = i, Description =


" Pin {i}"});
for(byte i = 1; i< = 10; i ++)col2.Add(new Pin(){Number = i,Description =
"Pin {i}" }); for (byte i = 1; i <= 10; i++) col2.Add(new Pin() { Number = i, Description =


" Pin {i }"});
}
private ObservableCollection< Pin> col1 = new ObservableCollection< Pin>();
private ObservableCollection< Pin> col2 = new ObservableCollection< Pin>();
private ObservableCollection< Pin> col3 = new ObservableCollection< Pin>();

public ObservableCollection< Pin> View1 {get {return col1; }

public Pin Selected1 {get;组; }

public ObservableCollection< Pin> View2 {get {return col2; }

public Pin Selected2 {get;组; }
public ObservableCollection< Pin> View3 {get {return col3;

公共ICommand Cmd
{
得到
{
返回新的RelayCommand((state)=>
{
if(Selected1!= null&& Selected2!= null)
col3.Add(new Pin()
{
Number = Selected1.Number,
Description = Selected1 .Description,
ConnectedNumber = Selected2.Number,
ConnectedDescription =
"Pin {i}" }); } private ObservableCollection<Pin> col1 = new ObservableCollection<Pin>(); private ObservableCollection<Pin> col2 = new ObservableCollection<Pin>(); private ObservableCollection<Pin> col3 = new ObservableCollection<Pin>(); public ObservableCollection<Pin> View1 { get { return col1; } } public Pin Selected1 { get; set; } public ObservableCollection<Pin> View2 { get { return col2; } } public Pin Selected2 { get; set; } public ObservableCollection<Pin> View3 { get { return col3; } } public ICommand Cmd { get { return new RelayCommand((state) => { if (Selected1 != null && Selected2 != null) col3.Add(new Pin() { Number = Selected1.Number, Description = Selected1.Description, ConnectedNumber = Selected2.Number, ConnectedDescription =


这篇关于使用按钮在Visual Studio 17中检索字典数据,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆