遍历两列WPF数据网格的行,并形成包含元素的列表集合 [英] Loop through the rows of a two column WPF datagrid and form a list collection with the elements

查看:69
本文介绍了遍历两列WPF数据网格的行,并形成包含元素的列表集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,在其中创建了一个列表集合,并成功地 binded 将其绑定到了DataGrid.我遇到的问题是,当用户在对DataGrid进行更改后单击SAVE按钮时,将数据从datagrid中拉回并使用它来形成同一列表集合的元素.

提前谢谢.

这是XAML 这是Xaml:

I am creating an application in which I created a list collection and successfully binded bound it to a DataGrid. The problem I am having is to pull data back from the datagrid and use it to form the elements of the same list collection when a user clicks the SAVE button after making changes to the DataGrid.

Thanks in advance.

THS IS THE XAML This is the Xaml:

<grid removed="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" AlternatingRowremoved="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"/>
        </datagridtextcolumn></datagridtextcolumn></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>



这是背后的代码:



This is the code behind:

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
        }
    }
}

推荐答案

您确实应该为dataGrid使用绑定.如果您具有以下类作为DataContext

You really should be using binding for the dataGrid. If you have the following class as the DataContext

public class DataContext : INotifyPropertyChanged
{
  Public List<values8> Source8 {
    get { return _source8;}
    set {
      if (value != _source8)
      {
        _source8 = value;
        if (PropertyChange != null)
          PropertyChanged (this, new PropertyChangedEventArgs(_source8));
      }
    }
  }
  private List<values8> _source8; 
  // Whatever else
}</values8></values8>



XAML将是:



The XAML would be:

<datagrid itemssource="{Binding Source8}">
  ......
</datagrid>



现在,要获取网格中的值,您只需要遍历Source8 列表即可.

您需要了解MVVM模式,这正是WPF想要使用的模式.



Now to get the values in the Grid, you just need to interate through the Source8 list.

You need to learn about the MVVM pattern, which is really what WPF is intended to work with.


这篇关于遍历两列WPF数据网格的行,并形成包含元素的列表集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆