WPF:C#如何获取Listbox的选定值?

Moh*_*ava -1 c# wpf listbox

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

我的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
Run Code Online (Sandbox Code Playgroud)

建议我怎么做.


编辑

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 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

pus*_*raj 5

因为您使用匿名类型来填充列表框,所以您没有选项,而是一个object类型来转换SelectedItem为.其他方法可能包括反射以提取字段/属性值.

但如果您使用的是C#4或更高版本,则可以使用 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:

    }
Run Code Online (Sandbox Code Playgroud)

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