Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 如何访问GroupItem中的控件?_C#_Wpf - Fatal编程技术网

C# 如何访问GroupItem中的控件?

C# 如何访问GroupItem中的控件?,c#,wpf,C#,Wpf,我正在使用CollectionView将我的项目分组如下: <CollectionViewSource Source="{Binding Competitions}" x:Key="GroupedData"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="Item.Country" /> <

我正在使用
CollectionView
将我的项目分组如下:

<CollectionViewSource Source="{Binding Competitions}" x:Key="GroupedData">
      <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Item.Country" />
       </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
public class League
{
    public string Name { get; set; }
    public Country Country { get; set; }
}

public class Country
{
   public string Name { get; set; } 
   public string ISO { get; set; } 
}
我正确地得到了组,但是
控件
变量甚至是
Null
,代码似乎找不到
部分显示联盟
:与容器标题中的
国家
关联的复选框

注:

league
是以下内容的简单实现:

<CollectionViewSource Source="{Binding Competitions}" x:Key="GroupedData">
      <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Item.Country" />
       </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
public class League
{
    public string Name { get; set; }
    public Country Country { get; set; }
}

public class Country
{
   public string Name { get; set; } 
   public string ISO { get; set; } 
}
我做错了什么

更新

忘记添加集合实现,尤其是
竞争

public List<CheckedListItem<League>> Competitions = new List<CheckedListItem<League>>();
UI结构将包括以下内容:

[] Italy     <- this is the GroupHeader sort by xaml
    [] foo   <- Items of the container
    [] foo2  
[] England
    [] foo3

[]意大利发布代码,有人可以复制粘贴并重现您的问题,这样人们不仅可以解决您的问题,还可以提出更好的解决方案

同时,您可以通过以下方式找到您的复选框:

var control = FindVisualChildren<CheckBox>(container);
var control=FindVisualChildren(容器);


编辑:一个完整的工作示例

public class League
{
    public string Name { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    public string Name { get; set; }
    public string ISO { get; set; }
}
public class CheckedListItem<T> 
{
    private bool isChecked;
    private T item;

    public CheckedListItem() { }

    public CheckedListItem(T item, bool isChecked = false)
    {
        this.item = item;
        this.isChecked = isChecked;
    }

    public T Item
    {
        get { return item; }
        set
        {
            item = value;

        }
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;

        }
    }
}
 public partial class MainWindow : Window 
{
    public ObservableCollection<CheckedListItem<League>> Competitions { get; set; }
    public MainWindow()
    {
        InitializeComponent();


        Competitions = new ObservableCollection<CheckedListItem<League>> { (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo", Country = new Country { Name = "Italy" } }
        }),
        (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo2", Country = new Country { Name = "Italy" } }

        }),
        (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo", Country = new Country { Name = "England" } }
        }) };

        this.DataContext = this;
    }

    public void CheckBoxCountry_Checked(object sender, EventArgs args)
    {
        foreach (CollectionViewGroup gp in CompetitionCombo.Items.Groups)
        {

                //Get the container
                GroupItem container = CompetitionCombo.ItemContainerGenerator.ContainerFromItem(gp) as GroupItem;

            //Get the control
            var control = FindVisualChildren<CheckBox>(container);



        }
    }

    public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                if (child != null && child is T)
                    yield return (T)child;

                foreach (T childOfChild in FindVisualChildren<T>(child))
                    yield return childOfChild;
            }
        }
    }
}
公共阶级联盟
{
公共字符串名称{get;set;}
公共国家{get;set;}
}
公营国家
{
公共字符串名称{get;set;}
公共字符串ISO{get;set;}
}
公共类CheckedListItem
{
私人住宅被检查;
私人物品;
public CheckedListItem(){}
公共CheckedListItem(T项,bool isChecked=false)
{
this.item=项目;
this.isChecked=isChecked;
}
公共交通项目
{
获取{return item;}
设置
{
项目=价值;
}
}
公共场所被检查
{
获取{return isChecked;}
设置
{
isChecked=值;
}
}
}
公共部分类主窗口:窗口
{
公开收集竞赛{get;set;}
公共主窗口()
{
初始化组件();
竞争=新的ObservableCollection{(新的CheckedListItem
{
IsChecked=true,
项目=新联盟{Name=“foo”,国家=新国家{Name=“意大利”}
}),
(新的CheckedListItem)
{
IsChecked=true,
项目=新联盟{Name=“foo2”,国家=新国家{Name=“意大利”}
}),
(新的CheckedListItem)
{
IsChecked=true,
项目=新联盟{Name=“foo”,国家=新国家{Name=“England”}
}) };
this.DataContext=this;
}
public void CheckBoxCountry_已选中(对象发送方、事件args args)
{
foreach(CollectionViewGroup gp in CompetitionCombo.Items.Group)
{
//拿容器
GroupItem容器=CompetitionCombo.ItemContainerGenerator.ContainerFromItem(gp)作为GroupItem;
//控制
var控制=FindVisualChildren(容器);
}
}
公共IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
for(int i=0;i
.xaml


我添加了
NormalItemTemplate
,因为您没有提供模板


当您选中UI中的复选框时,您将调用CheckBoxCountry_Checked in mainwindow.cs方法,该方法将在所有3个组合框中找到该复选框。

发布代码,有人可以复制粘贴并重现您的问题,这样人们不仅可以解决您的问题,还可以提出更好的解决方案

同时,您可以通过以下方式找到您的复选框:

var control = FindVisualChildren<CheckBox>(container);
var control=FindVisualChildren(容器);


编辑:一个完整的工作示例

public class League
{
    public string Name { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    public string Name { get; set; }
    public string ISO { get; set; }
}
public class CheckedListItem<T> 
{
    private bool isChecked;
    private T item;

    public CheckedListItem() { }

    public CheckedListItem(T item, bool isChecked = false)
    {
        this.item = item;
        this.isChecked = isChecked;
    }

    public T Item
    {
        get { return item; }
        set
        {
            item = value;

        }
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;

        }
    }
}
 public partial class MainWindow : Window 
{
    public ObservableCollection<CheckedListItem<League>> Competitions { get; set; }
    public MainWindow()
    {
        InitializeComponent();


        Competitions = new ObservableCollection<CheckedListItem<League>> { (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo", Country = new Country { Name = "Italy" } }
        }),
        (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo2", Country = new Country { Name = "Italy" } }

        }),
        (new CheckedListItem<League>
        {
            IsChecked = true,
            Item = new League { Name = "foo", Country = new Country { Name = "England" } }
        }) };

        this.DataContext = this;
    }

    public void CheckBoxCountry_Checked(object sender, EventArgs args)
    {
        foreach (CollectionViewGroup gp in CompetitionCombo.Items.Groups)
        {

                //Get the container
                GroupItem container = CompetitionCombo.ItemContainerGenerator.ContainerFromItem(gp) as GroupItem;

            //Get the control
            var control = FindVisualChildren<CheckBox>(container);



        }
    }

    public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

                if (child != null && child is T)
                    yield return (T)child;

                foreach (T childOfChild in FindVisualChildren<T>(child))
                    yield return childOfChild;
            }
        }
    }
}
公共阶级联盟
{
公共字符串名称{get;set;}
公共国家{get;set;}
}
公营国家
{
公共字符串名称{get;set;}
公共字符串ISO{get;set;}
}
公共类CheckedListItem
{
私人住宅被检查;
私人物品;
public CheckedListItem(){}
公共CheckedListItem(T项,bool isChecked=false)
{
this.item=项目;
this.isChecked=isChecked;
}
公共交通项目
{
获取{return item;}
设置
{
项目=价值;
}
}
公共场所被检查
{
获取{return isChecked;}
设置
{
isChecked=值;
}
}
}
公共部分类主窗口:窗口
{
公开收集竞赛{get;set;}
公共主窗口()
{
初始化组件();
竞争=新的ObservableCollection{(新的CheckedListItem
{
IsChecked=true,
项目=新联盟{Name=“foo”,国家=新国家{Name=“意大利”}
}),
(新的CheckedListItem)
{
IsChecked=true,
项目=新联盟{Name=“foo2”,国家=新国家{Name=“意大利”}
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <CollectionViewSource Source="{Binding Competitions}" x:Key="GroupedData">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Item.Country" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>

            <DataTemplate x:Key="NormalItemTemplate">
                <DockPanel>
                    <TextBlock Text="sdf" Margin="10,0,0,0" Foreground="Black" FontWeight="Bold"/>
                </DockPanel>
            </DataTemplate>
            <DataTemplate x:Key="CombinedTemplate">
                <ContentPresenter x:Name="Presenter" 
                          Content="{Binding}"
                                   ContentTemplate="{StaticResource NormalItemTemplate}"
                           />
            </DataTemplate>
            <DataTemplate x:Key="GroupHeader">
                <DockPanel>


                    <TextBlock Text="{Binding Name.Name}" Margin="10,0,0,0" Foreground="Black" FontWeight="Bold"/>
                    <CheckBox Margin="5,0,0,0" HorizontalAlignment="Right" IsChecked="True" x:Name="PART_DisplayLeague" 
                           Checked="CheckBoxCountry_Checked" />
                </DockPanel>
            </DataTemplate>

            <Style TargetType="{x:Type GroupItem}" x:Key="containerStyle">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander IsExpanded="False" x:Name="ComboExpander" 
                                  Header="{TemplateBinding Content}"
                                  HeaderTemplate="{StaticResource GroupHeader}" >
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <ComboBox x:Name="CompetitionCombo"
          ItemsSource="{Binding Source={StaticResource GroupedData}}"
          ItemTemplate="{StaticResource CombinedTemplate}">
            <ComboBox.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource containerStyle}"
                        HeaderTemplate="{StaticResource GroupHeader}">
                </GroupStyle>
            </ComboBox.GroupStyle>
        </ComboBox>
    </Grid>
</Window>