如何从DataTabale.AsDataView()中分组DataGrid哪些ItemsSource是DataView?

时间:2015-11-10 17:04:25

标签: wpf datagrid

我的代码几乎没问题,但我无法在扩展器中显示信息: - (

我的XAML:

<Window x:Class="RechercheOperateur.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="Recherche des opérateurs" Height="600" Width="800" Icon="../binoculars.png">

                                                                           

                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander Background="#FF4ABCE2" BorderBrush="Black" BorderThickness="1" Padding="5,5,5,5">
                                        <Expander.Header>
                                            <StackPanel  Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=FICLE, StringFormat={}Fiche mère n°{0}}" />
                                                <TextBlock Text="{Binding Path=FINOM, StringFormat={} - {0}}"/>
                                                <TextBlock Text="{Binding Path=ItemCount, StringFormat={} - ({0} éléments)}"/>
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />                   
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <DataGrid.ContextMenu>
            <ContextMenu >
                <MenuItem Name="AfficherFicheMere" Header="Afficher la fiche Mère / Contact" Click="AfficherFicheMere_OnClick" />
                <MenuItem Name="AfficherResume" Header="Afficher le résumé" Click="AfficherResume_OnClick"  />
            </ContextMenu>
        </DataGrid.ContextMenu>
        <DataGrid.Columns>
            <DataGridTextColumn Header="N°Inter" Width="80" Binding="{Binding Path=FICLE}"/>
            <DataGridTextColumn Header="N°Contact" Width="80" Binding="{Binding Path=No}"/>
            <DataGridTextColumn Header="Nom Société" Width="Auto" Binding="{Binding Path=FINOM}"/>
            <DataGridTextColumn Header="Nom Contact" Width="Auto" Binding="{Binding Path=FIPER}"/>
            <DataGridTextColumn Header="Enseigne" Width="200" Binding="{Binding Path=FIENS}"/>
            <DataGridTextColumn Header="Adresse 1" Width="200" Binding="{Binding Path=FIADR1}"/>
            <DataGridTextColumn Header="Adresse 2" Width="200" Binding="{Binding Path=FIADR2}"/>
            <DataGridTextColumn Header="Code Postal" Width="50" Binding="{Binding Path=FIPOST}"/>
            <DataGridTextColumn Header="Ville" Width="150" Binding="{Binding Path=FIADR3}"/>
            <DataGridTextColumn Header="Pays" Width="40" Binding="{Binding Path=Pays_Nom}"/>
            <DataGridTextColumn Header="Fixe" Width="90" Binding="{Binding Path=FITEL}"/>
            <DataGridTextColumn Header="Fax" Width="90" Binding="{Binding Path=FIFAX}"/>
            <DataGridTextColumn Header="Portable" Width="90" Binding="{Binding Path=FIMOBILE}"/>
            <DataGridTextColumn Header="Email" Width="200" Binding="{Binding Path=FIMAIL}"/>
            <DataGridTextColumn Header="Mémo" Width="200" Binding="{Binding Path=FIMEMO}"/>
            <DataGridTextColumn Header="N°Accise" Width="100" Binding="{Binding Path=FIACCI}"/>
            <DataGridTextColumn Header="N°CVI" Width="100" Binding="{Binding Path=FICVI}"/>
            <DataGridTextColumn Header="N°SIRET" Width="100" Binding="{Binding Path=FISIRET}"/>
            <DataGridTextColumn Header="Type" Width="30" Binding="{Binding Path=FITYPE}"/>
        </DataGrid.Columns>
    </DataGrid>

这是我的代码C#:

namespace RechercheOperateur
{

public partial class MainWindow
{
    private void BtnRechercher_OnClickechercher_Click(object sender, RoutedEventArgs e)
    {
        string searchPattern = TxbRecherche.Text;
        TbxStatus.Text = " Recherche de '" + searchPattern + "' en cours ...";
        cbx_fichesInactives.IsChecked = false;

        //Lancement de la fenêtre de chargement dans un autre thread..
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

        if (bw.IsBusy != true)
        {
            pbRechercheEnCours.Visibility = Visibility.Visible;
            lblRechercheEnCours.Visibility = Visibility.Visible;
            bw.RunWorkerAsync(searchPattern);
        }
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        var recherches = new Recherches(_connectionString);
        var resultDataSet = recherches.RechercherOperateur(e.Argument.ToString());

        if (resultDataSet != null)
            _listeOperateur = resultDataSet.Tables[0];
        else
            MessageBox.Show(
                "Impossible de récupérer les données. Veuillez contacter votre administrateur et regarder les logs.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (_listeOperateur != null)
        {
            EnumerableRowCollection<DataRow> query = from operateur in _listeOperateur.AsEnumerable()
                where operateur.Field<bool>("FIACT") == true
                select operateur;

            ResultsDataGrid.DataContext = query.AsDataView();

            ICollectionView view = CollectionViewSource.GetDefaultView(ResultsDataGrid.DataContext);
            view.GroupDescriptions.Clear();
            view.GroupDescriptions.Add(new PropertyGroupDescription("FICLE"));

            TbxStatus.Text = query.AsDataView().Count + " résultats.";
        }
        else
            TbxStatus.Text = "Erreur lors de la récupération des données";
        pbRechercheEnCours.Visibility = Visibility.Hidden;
        lblRechercheEnCours.Visibility = Visibility.Hidden;
    }
  }
}

我不知道为什么,但是当我启动我的应用程序时,除了我的Expander中的Stackpanel之外,一切都很完美:

<StackPanel  Orientation="Horizontal">
       <TextBlock FontWeight="Bold" Text="{Binding Path=FICLE, StringFormat={}Fiche mère n°{0}}" />
       <TextBlock Text="{Binding Path=FINOM, StringFormat={} - {0}}"/>
       <TextBlock Text="{Binding Path=ItemCount, StringFormat={} - ({0} éléments)}"/>
</StackPanel>

这是一个捕获: Empty expander

我不明白的是我的数据在这里,在下表中,但不在我的扩展器中...... 有什么建议吗?

由于

1 个答案:

答案 0 :(得分:1)

试试这个

<StackPanel  Orientation="Horizontal">
       <TextBlock FontWeight="Bold" Text="{Binding Path=Items[0].FICLE, StringFormat={}Fiche mère n°{0}}" />
       <TextBlock Text="{Binding Path=Items[0].FINOM, StringFormat={} - {0}}"/>
       <TextBlock Text="{Binding Path=ItemCount, StringFormat={} - ({0} éléments)}"/>
</StackPanel>