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.
My ListBox
name is 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
Suggest me how to do that.
EDIT
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>