0

行ごとに記事を追加し、必要に応じて削除できる既存のコードを変更しようとしています。問題は、同じカテゴリの記事をグループ化する方法を見つけようとしていることです。したがって、ユーザーがDVDカテゴリの新しい記事を追加すると、そのカテゴリに直接追加されます(ここからいくつかのアイデアを取り入れようとしましたが、成功しませんでした:http: //msdn.microsoft.com/en-us/library /ff407126.aspx

これはデータの背後にあるコードです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfDataGridWithDataTable
{
    public class Article
    {
        public  Article ()
        {

        }
        private int  _modelNumber;
        public int ModelNumber
        {
            get { return _modelNumber; }
            set { _modelNumber = value; OnPropertyChanged("ModelNumber"); }
        }

        private string _modelName;
        public string ModelName
        {
            get { return _modelName; }
            set { _modelName = value; OnPropertyChanged("ModelName"); }
        }

        private decimal  _unitCost;
        public decimal UnitCost
        {
            get { return _unitCost; }
            set { _unitCost = value; OnPropertyChanged("UnitCost"); }
        }

        private string  _description ;
        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged("Description"); }
        }


        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion
    }
    public class ListArticles : ObservableCollection<Article > 
    {
        public Article a;
        public ListArticles()
        {

                a = new Article();
                this.Add(a);

        }

    }

}

XAMLコードは次のとおりです。

<Window x:Class="WpfDataGridWithDataTable.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataGridWithDataTable"
        Title="Window1" Height="300" Width="300">
    <Grid
        Name="gridPanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DataGrid 
            Grid.Column="0"
            Name="dataGrid1"  
            AutoGenerateColumns="True"  
            CanUserAddRows="True" 
            CanUserDeleteRows="True"
            CanUserResizeColumns="True"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}"/>
        <ListBox 
            Grid.Column="1"
            Name="listBox1" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Article}">
                    <StackPanel    
                        Orientation="Horizontal">
                        <TextBlock 
                            Width="100"
                            Margin="10"  
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelNumber}"/>
                        <TextBlock 
                            Width="100"
                            Margin="10" 
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
            <Button 
                Grid.Row="1"
                Grid.Column="0"
                HorizontalAlignment="Left"
                Width="100"
                Name="btnAdd"
                Content="Add Item"
                Click="btnAdd_Click">
            </Button>
            <Button
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Right"
                Width="100"
                Name="btnDelete"
                Content="Delete Item"
                Click="btnDelete_Click" >
            </Button>
    </Grid>
</Window>

そして、フォームの背後にあるコード:

namespace WpfDataGridWithDataTable
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private ListArticles myList;
        public Window1()
        {
            InitializeComponent();
            myList = new ListArticles();


            this.DataContext = myList;

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(new Article());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            myList.Remove(this.dataGrid1.SelectedItem as Article);
        }

    }
}

回答ありがとうございます。

4

1 に答える 1

0

ようやく問題は解決しましたが、なぜうまくいかなかったのかわかりません。この問題は、データグリッドをCollectionViewSourceにバインドする際のXAMLコードにありました。置いた:

 <DataGrid Name="dataGrid1"  
            AutoGenerateColumns="True"  
            CanUserAddRows="True" 
            CanUserDeleteRows="True"
            CanUserResizeColumns="True"
            IsSynchronizedWithCurrentItem="True"
            Grid.ColumnSpan="2" 
            ItemsSource="{Binding Source={StaticResource cvsListArticles}}">

最後の行が問題でした。簡単に言えItemsSource="{Binding}"ば、最終的には機能しました。

同じ問題を抱えている可能性があり、その恩恵を受けたい人のためのコードは次のとおりです。

データの背後にあるコード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfDataGridWithDataTable
{
    public class Article
    {
        public Article()
        {

        }
        private int _modelNumber;
        public int ModelNumber
        {
            get { return _modelNumber; }
            set { _modelNumber = value; OnPropertyChanged("ModelNumber"); }
        }

        private string _modelName;
        public string ModelName
        {
            get { return _modelName; }
            set { _modelName = value; OnPropertyChanged("ModelName"); }
        }

        private decimal _unitCost;
        public decimal UnitCost
        {
            get { return _unitCost; }
            set { _unitCost = value; OnPropertyChanged("UnitCost"); }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged("Description"); }
        }


        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion
    }
    public class ListArticles : ObservableCollection<Article>
    {
        public Article a;
        public ListArticles()
        {
            a = new Article();
            this.Add(a);

        }


    }

}

XAMLコード:

<Window x:Class="WpfDataGridWithDataTable.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication18"
        Title="Window1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="480" Width="760">

     <Window.Resources>
            <CollectionViewSource x:Key="cvsListArticles" Source="Article">

            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="ModelName"/>
            </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </Window.Resources> 


    <Grid
        Name="gridPanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DataGrid Name="dataGrid1"  
            AutoGenerateColumns="True"  
            CanUserAddRows="True" 
            CanUserDeleteRows="True"
            CanUserResizeColumns="True"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}" Grid.ColumnSpan="2" >

            <DataGrid.GroupStyle>

                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Margin" Value="0,0,0,5"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
                                            <Expander.Header>
                                                <DockPanel>
                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
                                                </DockPanel>
                                            </Expander.Header>
                                            <Expander.Content>
                                                <ItemsPresenter />
                                            </Expander.Content>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>

        <Button 
                Grid.Row="1"
                Grid.Column="0"
                HorizontalAlignment="Left"
                Width="100"
                Name="btnAdd"
                Content="Add Item"
                Click="btnAdd_Click">
        </Button>
        <Button
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Right"
                Width="100"
                Name="btnDelete"
                Content="Delete Item"
                Click="btnDelete_Click" >
        </Button>
        <Button Content="Group" Grid.ColumnSpan="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="294,5,0,0" Name="GroupButton" VerticalAlignment="Top" Width="145" Click="GroupButton_Click" />
    </Grid>
</Window>

フォームの背後にあるコード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfDataGridWithDataTable
{

    public partial class Window1 : Window
    {
        private ListArticles myList;
        public Window1()
        {
            InitializeComponent();



            myList = new ListArticles();


            this.DataContext = myList;

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(new Article());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            myList.Remove(this.dataGrid1.SelectedItem as Article);
        }

        private void GroupButton_Click(object sender, RoutedEventArgs e)
        {
            ICollectionView cvsListArticles = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
            if (cvsListArticles != null && cvsListArticles.CanGroup == true)
            {
                cvsListArticles.GroupDescriptions.Clear();
                cvsListArticles.GroupDescriptions.Add(new PropertyGroupDescription("ModelName"));
            }
        }


    }
}
于 2012-09-11T20:24:57.257 に答える