Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 使用MVVM在WPF中动态列和分组_C#_Wpf_Mvvm_Datagrid - Fatal编程技术网

C# 使用MVVM在WPF中动态列和分组

C# 使用MVVM在WPF中动态列和分组,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,也许你可以帮我解决我的问题:虽然我找到了各种方法、链接以及动态列或分组的方法,但我没有找到将它们结合起来的方法。 我将进一步解释我的项目目标: 我想用DataGrid显示一些关于体育比赛的信息。想象一个事件,在该事件中,多个玩家进行不同的运动并最终获得结果,因此我的datagrid标题看起来像: 体育|运动员|发球台号码|比赛X |比赛Y |比赛Z | 我的数据源为我提供了一个结果列表{Sport,Player,teennumber,GameNumber,Result},从中我生成了一个Play

也许你可以帮我解决我的问题:虽然我找到了各种方法、链接以及动态列或分组的方法,但我没有找到将它们结合起来的方法。 我将进一步解释我的项目目标: 我想用DataGrid显示一些关于体育比赛的信息。想象一个事件,在该事件中,多个玩家进行不同的运动并最终获得结果,因此我的datagrid标题看起来像:

体育|运动员|发球台号码|比赛X |比赛Y |比赛Z |

我的数据源为我提供了一个结果列表{Sport,Player,teennumber,GameNumber,Result},从中我生成了一个PlayerResults{Sport,Player,teennumber{GameNumber,Result}列表

现在我想为一个游戏显示一个可配置数量的结果,我已经通过DataTable完成了,在DataTable中我添加了代码隐藏中的列。 我现在如何认识到按运动分组,最好是像扩展器一样

注:不同的运动项目的游戏号码并不不同,对于给定的运动、玩家和游戏号码组合,只有一个或没有结果

编辑:现在通过一些修改和简单的想法,即DataTable的默认视图是List类型的,这足以使CollectionView可用,我完成了我的基本项目想法

好的,我的模型给了我一个列表:

public class GameValue
{
    public string Sport;
    public string Player;
    public string TeeNumber;
    public string GameNumber;
    public string GameResult;
    public DateTime GameDate;
}
我将其转换为ViewModel中的不同ObservableCollection,即基于这些类的Games和PlayerResults,它们继承了实现INotifyPropertyChanged的ObservableObject类:

public class Game : ObservableObject
    {
    private string mGameNumber;
    private DateTime mGameDate;

    public string GameNumber
    {
        get { return mGameNumber; }
        set
        {
            mGameNumber = value;
            RaisePropertyChangedEvent("GameNumber");
        }
    }

    public DateTime GameDate
    {
        get { return mGameDate; }
        set
        {
            mGameDate = value;
            RaisePropertyChangedEvent("GameDate");
        }
    }
}
还有我的PlayerResult课程:

public class PlayerResult: Player
{
    private ObservableCollection<GameResult> mGameResults;

    public ObservableCollection<GameResult> GameResults
    {
        get { return mGameResults; }
        set
        {
            mGameResults = value;
            RaisePropertyChangedEvent("GameResults");
        }
    }

    public PlayerResult (List<GameResult> pGameResults, Player pPlayer )
    {
        Name = pPlayer.Name;
        TeeNumber = pPlayer.TeeNumber;
        Sport = pPlayer.Sport;
        GameResults = new ObservableCollection<GameResult>(pGameResults);

    }


    public String ContainsResultForGame(string mGameNumber)
    {

        var tmpResults = GameResults.Where(p => p.GameNumber == mGameNumber);
        if (tmpResults.FirstOrDefault() == null)
            return "";
        return tmpResults.First().GameNumber;
    }
}
这是我的XAML:

<Window x:Class="WFP_Sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Window.Resources>
            <CollectionViewSource x:Key="cvsPlayerResults" Source="{Binding PlayerTable}" >
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Sport"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Window.Resources>

        <Grid>
            <DataGrid x:Name="TableauGrid" AutoGenerateColumns="True"  CanUserReorderColumns="False" CanUserAddRows="false" ItemsSource="{Binding Source={StaticResource cvsPlayerResults}}">
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" >
                                            <Expander.Header>
                                                <TextBlock  Text="{Binding Path=Sport}"/>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter />
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
            </DataGrid>
        </Grid>
</Window>

新问题:如果扩展器已启用,如何更改组样式,使“运动”显示在扩展器旁边


作为示例,我可以将我的google代码项目的链接(希望能够在不同的计算机上工作)发送给你。

显示你的代码,我们可能会提出一些建议。@Sheridan补充了我代码的重要部分。如果您需要更多,请随时添加其他评论等。提前感谢。
public MainWindow()
{
        InitializeComponent();
        DataContext = new MainViewModel();
} 
<Window x:Class="WFP_Sandbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Window.Resources>
            <CollectionViewSource x:Key="cvsPlayerResults" Source="{Binding PlayerTable}" >
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Sport"/>
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Window.Resources>

        <Grid>
            <DataGrid x:Name="TableauGrid" AutoGenerateColumns="True"  CanUserReorderColumns="False" CanUserAddRows="false" ItemsSource="{Binding Source={StaticResource cvsPlayerResults}}">
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" >
                                            <Expander.Header>
                                                <TextBlock  Text="{Binding Path=Sport}"/>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter />
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
            </DataGrid>
        </Grid>
</Window>