Image of the list box is as follows:
Listbox with Dynamic column with check boxes and static radio buttons
I have to look for the selected items from list box and selected radio button for each selected item
<ListBox Height="226" HorizontalAlignment="Left" Margin="404,339,0,0" Name="listBoxItems" VerticalAlignment="Top" Width="282" SelectionChanged="listBoxItems_SelectionChanged" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,5,0,0">
<CheckBox Width="130" x:Name="chbPrescr" Content="{Binding}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<!--<TextBlock Text="{Binding}" Width="115"></TextBlock>-->
</CheckBox>
<RadioButton x:Name="rdOD" Width="40" Content ="OD" Checked="rdOD_Checked" />
<RadioButton x:Name="rdBD" Width="40" Content ="BD" Checked="rdBD_Checked"/>
<RadioButton x:Name="rdTDS" Width="40" Content ="TDS" Checked="rdTDS_Checked"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And at the postback event I am getting selected list item but not the selected radio button for that selected list item
foreach (var items in listBoxItems.SelectedItems)
{
string a = items.ToString();
ItemCollection itemCollection = listBoxItems.Items;
object currentItem = itemCollection.CurrentItem;
}
I need to know how can I get the category for each of the selected medicine. Thanks
You could bind the IsChecked
property of the RadioButtons
to a source property of your data class:
WPF + MVVM + RadioButton : Handle binding with single property
This is the preferable solution.
The fastest one is probably to find the selected RadioButton
in the visual tree:
private void listBoxItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var items in listBoxItems.SelectedItems)
{
ListBoxItem lbi = listBoxItems.ItemContainerGenerator.ContainerFromItem(items) as ListBoxItem;
if (lbi != null)
{
RadioButton rb = FindVisualChildren<RadioButton>(lbi).FirstOrDefault(x => x.IsChecked == true);
if (rb != null)
{
MessageBox.Show("selected: " + rb.Content.ToString());
}
}
}
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
int NbChild = VisualTreeHelper.GetChildrenCount(depObj);
for (int i = 0; i < NbChild; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childNiv2 in FindVisualChildren<T>(child))
{
yield return childNiv2;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.