代码之家  ›  专栏  ›  技术社区  ›  Zubair

如何从wpf列表框中获取选定列表项中的选定单选按钮

  •  1
  • Zubair  · 技术社区  · 7 年前

    Listbox with Dynamic column with check boxes and static radio buttons

    <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>
    

    在回发事件中,我得到的是选中的列表项,但不是该选中列表项的选中单选按钮

    foreach (var items in listBoxItems.SelectedItems)
    { 
        string a = items.ToString();
        ItemCollection itemCollection = listBoxItems.Items;
        object currentItem = itemCollection.CurrentItem;
    }
    

    我需要知道如何获得每种选定药物的类别。谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   mm8    7 年前

    您可以绑定 IsChecked RadioButtons 到数据类的源属性:

    WPF + MVVM + RadioButton : Handle binding with single property

    这是更好的解决方案。

    RadioButton 在视觉树中:

    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;
                }
            }
        }
    }