WPF:C#如何获取列表框的选定值? [英] WPF: C# How to get selected value of Listbox?

查看:175
本文介绍了WPF:C#如何获取列表框的选定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取列表框的选定值.当我实际尝试LstGroup1.SelectedItem时,我得到的值是{ BoothID = "4", BoothName = "HP" },即使我尝试从LstGroup1.SelectedValue中得到值,其输出也是一样的.现在我想获取BoothID,即预期的输出为4,但我无法获取.

I am trying to fetch the selected value of the listbox. When I actually try LstGroup1.SelectedItem I get the value { BoothID = "4", BoothName = "HP" } and even if i try to get the value from LstGroup1.SelectedValue the output is same. Now I want to fetch BoothID i.e. the expected output is 4 but i am unable to get so.

我的ListBox名称是LstGroup1.

public List<object> BoothsInGroup1 { get; set; }
// Inside the Constructor 
BoothsInGroup1 = new List<object>();
//After Fetching the value add 
BoothsInGroup1.Add(new { BoothID = da["BoothID"].ToString(), BoothName = da["BoothName"].ToString() });

//Now here I get 
var Booth = (LstGroup1.SelectedItem);
//Output is { BoothID = "4", BoothName = "HP" }

// Expected Output 4

建议我怎么做.

public partial class VotingPanel : Window
{
    public List<object> BoothsInGroup1 { get; set; }
    public VotingPanel()
    {
        InitializeComponent();
        DataContext = this;
        BoothsInGroup1 = new List<object>();

        //Connection is done
        SqlConnection con = new SqlConnection(ConnectionString);
        con.Open();

        FieldNameCmd.CommandText = "SELECT * FROM Booth b, BoothGroup bg where bg.GroupID=b.GroupID;";
        IDataReader da = FieldNameCmd.ExecuteReader();
        while (da.Read())
        {
            if (Group1Name.Text == da["GroupName"].ToString())
            { // Adds value to BoothsInGroup1
                BoothsInGroup1.Add(new { BoothID = da["BoothID"].ToString(), BoothName = da["BoothName"].ToString() });
            }
        }
    }
    private void BtnVote_Click(object sender, RoutedEventArgs e) // on Click wanted to find the value of Selected list box
    {   
        if (LstGroup1.SelectedIndex >= 0 && LstGroup2.SelectedIndex >= 0)
        {
            var Booth = (LstGroup1.SelectedItem);
            //Few things to do 
        }
    }
}

XAML

<ListBox Grid.Row="1"
         Name="LstGroup1"
         ItemsSource="{Binding BoothsInGroup1}"
         Margin="5,1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding BoothID}"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Bottom"
                           FontSize="15"
                           FontWeight="ExtraBold"
                           Margin="5,3"
                           Grid.Column="1" />
                <TextBlock Text="{Binding BoothName}"
                           Grid.Column="1"
                           VerticalAlignment="Center"
                           FontWeight="ExtraBold"
                           FontSize="30"
                           Margin="15" />

            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

推荐答案

,因为您使用匿名类型来填充列表框,所以您别无选择,只能使用object类型将SelectedItem强制转换为.其他方法可能包括反射以提取字段/属性值.

since you are using anonymous type to populate the listbox, so you have left with no option then an object type to cast the SelectedItem to. other approach may include Reflection to extract the field/property values.

但是如果您使用的是C#4或更高版本,则可以使用dynamic

but if you are using C# 4 or above you may leverage dynamic

    if (LstGroup1.SelectedIndex >= 0 && LstGroup2.SelectedIndex >= 0)
    {
        //use dynamic as type to cast your anonymous object to
        dynamic Booth = LstGroup1.SelectedItem as dynamic;
        //Few things to do 

        //you may use the above variable as
        string BoothID = Booth.BoothID;
        string BoothName = Booth.BoothName:

    }

这可能会解决您当前的问题,但出于多种原因,我建议出于同样的原因创建一个类.

this may solve your current issue, but I would suggest to create a class for the same for many reasons.

这篇关于WPF:C#如何获取列表框的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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