Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从wpf列表框中获取选定列表项中的选定单选按钮_C#_Wpf_Listbox_Wpf Controls - Fatal编程技术网

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

C# 如何从wpf列表框中获取选定列表项中的选定单选按钮,c#,wpf,listbox,wpf-controls,C#,Wpf,Listbox,Wpf Controls,列表框的图像如下所示: 我必须从列表框中查找所选项目,并为每个所选项目选择单选按钮 <ListBox Height="226" HorizontalAlignment="Left" Margin="404,339,0,0" Name="listBoxItems" VerticalAlignment="Top" Width="282" SelectionChanged="listBoxItems_SelectionChanged" SelectionMode="Multiple">

列表框的图像如下所示:

我必须从列表框中查找所选项目,并为每个所选项目选择单选按钮

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

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

您可以将
单选按钮的
IsChecked
属性绑定到数据类的源属性:

这是更好的解决方案

最快的方法可能是在可视树中找到所选的
单选按钮

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;
            }
        }
    }
}
private void listBoxItems\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
foreach(listBoxItems.SelectedItems中的变量项)
{
ListBoxItem lbi=listBoxItems.ItemContainerGenerator.ContainerFromItem作为ListBoxItem;
如果(lbi!=null)
{
RadioButton rb=FindVisualChildren(lbi).FirstOrDefault(x=>x.IsChecked==true);
如果(rb!=null)
{
Show(“选中:+rb.Content.ToString());
}
}
}
}
私有静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
int NbChild=visualtreeheloper.GetChildrenCount(depObj);
for(int i=0;i
您可以将
单选按钮的
IsChecked
属性绑定到数据类的源属性:

这是更好的解决方案

最快的方法可能是在可视树中找到所选的
单选按钮

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;
            }
        }
    }
}
private void listBoxItems\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
foreach(listBoxItems.SelectedItems中的变量项)
{
ListBoxItem lbi=listBoxItems.ItemContainerGenerator.ContainerFromItem作为ListBoxItem;
如果(lbi!=null)
{
RadioButton rb=FindVisualChildren(lbi).FirstOrDefault(x=>x.IsChecked==true);
如果(rb!=null)
{
Show(“选中:+rb.Content.ToString());
}
}
}
}
私有静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
int NbChild=visualtreeheloper.GetChildrenCount(depObj);
for(int i=0;i
您已经在使用一些绑定。为什么不绑定单选按钮呢?然后在viewmodels中有了这些值。您已经使用了一些绑定。为什么不绑定单选按钮呢?然后在viewmodels中有这些值。