2012-04-07 7 views
0

私はリストコレクションを作成し、それをデータグリッドにバインドしたアプリケーションを作成しています。私が抱えている問題は、データグリッドからデータを取り戻し、ユーザーがデータグリッドを変更した後にSAVEボタンをクリックしたときに同じリストコレクションの要素を形成することです。 ありがとうございます。 XAMLを以下に示し2列のWPFデータグリッドの行をループし、要素を含むリストコレクションを作成します。

<Grid Background="CornflowerBlue"> 
    <DataGrid AutoGenerateColumns="False" Height="530" HorizontalAlignment="Left" Margin="12,12,0,0" Name="gridBeveragesAndJuices" VerticalAlignment="Top" Width="573" 
       HorizontalGridLinesBrush="Cyan" RowBackground="#FF12AD12" VerticalGridLinesBrush="Cyan" Foreground="Cyan" EnableColumnVirtualization="False" 
       EnableRowVirtualization="False" CanUserDeleteRows="True" CanUserAddRows="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" AlternatingRowBackground="DodgerBlue" FontSize="13"> 
     <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <Setter Property="Foreground" Value="DodgerBlue" /> 
       <Setter Property="BorderBrush" Value="Yellow" /> 
      </Style> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTextColumn Width="287" Header="      ITEMS" 
         Binding="{Binding Path=ITEMS8, Mode=TwoWay, NotifyOnTargetUpdated=True}" x:Name="ItemsColumn" /> 
      <DataGridTextColumn Width="286" Header="      PRICE" 
         Binding="{Binding Path=PRICE8, Mode=TwoWay, NotifyOnTargetUpdated=True}" x:Name="PriceColumn"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="67,578,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" /> 
    <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="256,578,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" /> 
    <Button Content="Exit" Height="23" HorizontalAlignment="Left" Margin="444,578,0,0" Name="btnExit" VerticalAlignment="Top" Width="75" /> 


</Grid> 

以下は背後にあるコードです:

public partial class BeveragesAndJuices : Window 
{ 
    public BeveragesAndJuices() 
    { 
     InitializeComponent(); 
     gridBeveragesAndJuices.ItemsSource = Source8; 
    } 

    public class Values8 
    { 
     public string ITEMS8 { get; set; } 
     public decimal PRICE8 { get; set; } 
    } 


    public List<Values8> Source8 = new List<Values8> 
    { 
     new Values8(){ITEMS8 = "Lacasera ", PRICE8 = 290}, new Values8(){ITEMS8 = "Cran-Orange Chiller ", PRICE8 = 290}, new Values8(){ITEMS8 = "Festive Fruity Flavored Milk ", PRICE8 = 290}, 
     new Values8(){ITEMS8 = "Homemade Iced Coffee ", PRICE8 = 290}, new Values8(){ITEMS8 ="Lemon Cucumber Seltzer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Fizzy Water ", PRICE8 = 290}, 
     new Values8(){ITEMS8 ="Haunted (Black Cauldron) Punch " , PRICE8 = 290}, new Values8(){ITEMS8 ="Lemon Ginger Iced Green Tea " , PRICE8 = 290}, new Values8(){ITEMS8 ="Orange Creamsicle Shake " , PRICE8 = 290}, 
     new Values8(){ITEMS8 = "Blueberry Blast Smoothie ", PRICE8 = 290}, new Values8(){ITEMS8 ="Shamrock Milk Mixer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Pomegranate Punch ", PRICE8 = 290}, 
     new Values8(){ITEMS8 ="anned Milo " , PRICE8 = 290}, new Values8(){ITEMS8 ="Viju Milk " , PRICE8 = 290}, new Values8(){ITEMS8 = "5 Alive ", PRICE8 = 290}, 
     new Values8(){ITEMS8 ="Cherry Vanilla Smoothie " , PRICE8 = 290}, new Values8(){ITEMS8 = "Boysenberry-Banana Blast ", PRICE8 = 290}, new Values8(){ITEMS8 = "Vanilla Iced Mochaccino ", PRICE8 = 290}, 
     new Values8(){ITEMS8 ="Choco-Nana Milk Mixer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Fresh Fruit Pudding Milk Mixer ", PRICE8 = 290}, new Values8(){ITEMS8 ="Luscious Licuado " , PRICE8 = 290}, 
     new Values8(){ITEMS8 = "Frosty Pine-Orange Yogurt Smoothie ", PRICE8 = 290}, new Values8(){ITEMS8 = "Mocha-ccino Freeze ", PRICE8 = 290}, new Values8(){ITEMS8 ="Lite Iced Mocha " , PRICE8 = 290}, 
     new Values8(){ITEMS8 ="Nectarine Whirl " , PRICE8 = 290}, new Values8(){ITEMS8 ="Strawberries and Cream Smoothie " , PRICE8 = 290}, new Values8(){ITEMS8 ="Strawberry Light Lemonade " , PRICE8 = 290} 
    }; 

    private void btnSave_Click(object sender, RoutedEventArgs e) 
    { 
     //clear the elements of the source 
     Source8.Clear(); 

     //get the items on the datagrid and use them to form new elements of the 
     //Source8 

     foreach(DataGridRow Row in gridBeveragesAndJuices) 
     { 
      //this where am stuck, foreach flags an error 
      //that DataGridRow does not have a definition for GetEnumerator 
     } 
    } 
} 
+3

SOは、タイトルに小文字を使用することもできます。だから、聴衆は叫び声の人を好まない。 –

+1

-1を入力します。申し訳ありませんが、それはあまりにも空腹のためです。 – flq

答えて

0

ITEMS8とPRICE8のためのあなたのTwoWayBindingが仕事をしていませんので、Source8を再構築する必要はありません。値は既に最新です。たとえ新しいをDataGrid Source8に追加したとしても、直ちに新しいエントリが表示されます。

保存ボタンを使用してSource8をシリアル化します。これはDataGridの状態です。

+0

アイデアのおかげでLPLありがとう。このシリアル化をコードサンプルで説明してください。まだわかりません。 –

+0

[C#チュートリアル - XMLシリアル化](http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization)または[XMLシリアル化の紹介](http://msdn.microsoft.com/ en-us/library/182eeyhh%28v = vs.100%29.aspx)または[C#のバイナリシリアル化と逆シリアル化](http://www.ezzylearning.com/tutorial.aspx?tid=8802451)を参照してください。 – LPL

0

コードを少し変更してバインドします。新しいリストは毎回新しいリストになるので、リストを実際にパブリックプロパティにするべきではありません。= newは外部的に消費されることはありません。

private List<> source8 = new Values8(){ITEMS8 = "Lacasera ", PRICE8 = 290}, new Values8(){ITEMS8 = "Cran-Orange Chiller ", PRICE8 = 290}, new Values8(){ITEMS8 = "Festive Fruity Flavored Milk ", PRICE8 = 290}, 
    new Values8(){ITEMS8 = "Homemade Iced Coffee ", PRICE8 = 290}, new Values8(){ITEMS8 ="Lemon Cucumber Seltzer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Fizzy Water ", PRICE8 = 290}, 
    new Values8(){ITEMS8 ="Haunted (Black Cauldron) Punch " , PRICE8 = 290}, new Values8(){ITEMS8 ="Lemon Ginger Iced Green Tea " , PRICE8 = 290}, new Values8(){ITEMS8 ="Orange Creamsicle Shake " , PRICE8 = 290}, 
    new Values8(){ITEMS8 = "Blueberry Blast Smoothie ", PRICE8 = 290}, new Values8(){ITEMS8 ="Shamrock Milk Mixer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Pomegranate Punch ", PRICE8 = 290}, 
    new Values8(){ITEMS8 ="anned Milo " , PRICE8 = 290}, new Values8(){ITEMS8 ="Viju Milk " , PRICE8 = 290}, new Values8(){ITEMS8 = "5 Alive ", PRICE8 = 290}, 
    new Values8(){ITEMS8 ="Cherry Vanilla Smoothie " , PRICE8 = 290}, new Values8(){ITEMS8 = "Boysenberry-Banana Blast ", PRICE8 = 290}, new Values8(){ITEMS8 = "Vanilla Iced Mochaccino ", PRICE8 = 290}, 
    new Values8(){ITEMS8 ="Choco-Nana Milk Mixer " , PRICE8 = 290}, new Values8(){ITEMS8 = "Fresh Fruit Pudding Milk Mixer ", PRICE8 = 290}, new Values8(){ITEMS8 ="Luscious Licuado " , PRICE8 = 290}, 
    new Values8(){ITEMS8 = "Frosty Pine-Orange Yogurt Smoothie ", PRICE8 = 290}, new Values8(){ITEMS8 = "Mocha-ccino Freeze ", PRICE8 = 290}, new Values8(){ITEMS8 ="Lite Iced Mocha " , PRICE8 = 290}, 
    new Values8(){ITEMS8 ="Nectarine Whirl " , PRICE8 = 290}, new Values8(){ITEMS8 ="Strawberries and Cream Smoothie " , PRICE8 = 290}, new Values8(){ITEMS8 ="Strawberry Light Lemonade " , PRICE8 = 290}; 

public BeveragesAndJuices() 
{ 
    InitializeComponent(); 
    gridBeveragesAndJuices.ItemsSource = Source8; // move this to XAML Binding 
} 

public class Values8 
{ 
    public string ITEMS8 
    { 
     get; 
     set 
     { 
      Debug.WriteLine(value); 
      // this is where you do the update 
     } 
    } 
    public decimal PRICE8 { get; set; } 
} 


public List<Values8> Source8 
{ 
    get { return source8; } 

} 

private void btnSave_Click(object sender, RoutedEventArgs e) 
{ 
    //clear the elements of the source 
    //Source8.Clear(); for sure you don't want to do this ! 

    //get the items on the datagrid and use them to form new elements of the 
    //Source8 

    // do this is the set 
    //foreach(DataGridRow Row in gridBeveragesAndJuices) 
    //{ 
     //this where am stuck, foreach flags an error 
     //that DataGridRow does not have a definition for GetEnumerator 
    //} 
} 
+0

お返事ありがとうございます。 SAVEボタンをクリックすると、DataGridの変更をSource8にどのように保存するのですか?私はまた、ユーザーがDataGrid経由でSource8に新しい要素を追加できるようにしたい。 –

+0

DataGridにバインドされているので、Source8を更新してください。デバッグ文を参照してください。 – Paparazzi

関連する問題