c#数据绑定之数据转化为信息的示例

 更新时间:2014年04月10日 14:29:12   作者:  
这篇文章主要介绍了c#数据绑定中的数据转化为信息的示例,需要的朋友可以参考下

目标界面:

XAML代码:

复制代码 代码如下:

<Grid Margin="2">
            <Grid.RowDefinitions>
                <RowDefinition  Height="Auto"/>
                <RowDefinition  Height="Auto"/>
                <RowDefinition  Height="Auto"/>
                <RowDefinition />
            </Grid.RowDefinitions>
            <GroupBox Header="Customer" Grid.Row="0" Padding="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="CustomerID" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="0" Grid.Column="1" Margin="2" Name="tbxCustomerID" Text="{Binding Path=CID}"/>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="CustomerName" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="1" Grid.Column="1" Margin="2" Name="tbxCustomerName" Text="{Binding Path=Name}"/>
                    <Button Grid.Row="2" Grid.Column="1" Content="Add New Customer" Margin="2" Name="btnAddCustomer" Padding="2" Click="btnAddCustomer_Click" />
                </Grid>
            </GroupBox>
            <GroupBox Header="Order" Grid.Row="1" Padding="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="OrderID" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="0" Grid.Column="1" Margin="2" Name="tbxOrderID" Text="{Binding Path=OID}"/>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="OrderName" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="1" Grid.Column="1" Margin="2" Name="tbxOrderName" Text="{Binding Path=Customer}"/>
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Subtotal" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="2" Grid.Column="1" Margin="2" Name="tbxSubtotal" Text="{Binding Path=Subtotal}"/>
                    <TextBlock Grid.Row="3" Grid.Column="0" Text="TaxRate" Margin="2" HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    <TextBox Grid.Row="3" Grid.Column="1" Margin="2" Name="tbxTaxRate" Text="{Binding Path=TaxRate}"/>
                    <Button Grid.Row="4" Grid.Column="1" Content="Add New Order" Margin="2" Name="btnAddOrder" Padding="2" Click="btnAddOrder_Click" />
                </Grid>
            </GroupBox>
            <ListView Name="lstDisplayCustomer" ItemsSource="{Binding}" Grid.Row="2"  Margin="2" MinHeight="150">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn  Header="CustomerID" DisplayMemberBinding="{Binding CID}"/>
                            <GridViewColumn Header="CustomerName" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Header="Total" DisplayMemberBinding="{Binding OrderTotals}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
            <ListView Name="lstDisplayOrder" ItemsSource="{Binding}" Grid.Row="3"  Margin="2" MinHeight="150">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                            <GridViewColumn  Header="OrderID" DisplayMemberBinding="{Binding OID}"/>
                            <GridViewColumn Header="Customer" DisplayMemberBinding="{Binding Customer}"/>
                            <GridViewColumn Header="Subtotal" DisplayMemberBinding="{Binding Subtotal}"/>
                            <GridViewColumn Header="TaxRate"  DisplayMemberBinding="{Binding TaxRate}" />
                            <GridViewColumn Header="Total"    DisplayMemberBinding="{Binding Total}"/>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>

C# 代码:

复制代码 代码如下:

DataSet business = NewData();

        public MainWindow()
        {
            InitializeComponent();

        }

        private static DataSet NewData()
        {
            //-----build the parent table and add some data
            DataTable customer = new DataTable("Customer");
            customer.Columns.Add("CID", typeof(Int32));
            customer.Columns.Add("Name", typeof(string));
            //-------build the child table and add some data.
            DataTable orders = new DataTable("Order");
            orders.Columns.Add("OID", typeof(int));
            orders.Columns.Add("Customer", typeof(Int32));
            orders.Columns.Add("Subtotal", typeof(decimal));
            orders.Columns.Add("TaxRate", typeof(decimal));
            orders.Columns.Add("Total",typeof(decimal),"Subtotal*(1+TaxRate)");

            //-----Link the table within a Dataset.
            DataSet business = new DataSet();
            business.Tables.Add(customer);
            business.Tables.Add(orders);
            business.Relations.Add(customer.Columns["CID"],orders.Columns["Customer"]);
            customer.Columns.Add("OrderTotals" ,typeof(decimal),"Sum(Child.Total)");
            return business;
        }

        private void btnAddCustomer_Click(object sender, RoutedEventArgs e)
        {
           //Vist datatable customer.
            DataTable customer=business.Tables["Customer"];
            NewMember(customer);
            lstDisplayCustomer.DataContext = customer;
        }

        private DataTable NewMember(DataTable table)
        {
            DataRow newRow = table.NewRow();
            newRow["CID"] = tbxCustomerID.Text;
            newRow["Name"] = tbxCustomerName.Text;
            table.Rows.Add(newRow);
            return table;
        }

        private DataTable NewMemberOrder(DataTable table)
        {
            DataRow newRow = table.NewRow();
            newRow["OID"] = tbxOrderID.Text;
            newRow["Customer"] = tbxOrderName.Text;
            newRow["Subtotal"] = tbxSubtotal.Text;
            newRow["TaxRate"] = tbxTaxRate.Text;
            table.Rows.Add(newRow);
            return table;
        }

        private void btnAddOrder_Click(object sender, RoutedEventArgs e)
        {
            //Vist datatable order.
            DataTable order = business.Tables["Order"];
            NewMemberOrder(order);
            lstDisplayOrder.DataContext = order;
        }

相关文章

  • C#操作ini文件的帮助类

    C#操作ini文件的帮助类

    这篇文章介绍了C#操作ini文件的帮助类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#中静态方法和实例化方法的区别、使用

    C#中静态方法和实例化方法的区别、使用

    这篇文章主要介绍了C#中静态方法和实例化方法的区别、使用,文中讲解的非常细致,对大家的学习有所帮助,感兴趣的朋友可以了解下
    2020-06-06
  • c# this关键字用法代码详解

    c# this关键字用法代码详解

    在本篇文章里小编给大家整理的是关于c# this关键字用法以及相关实例代码,有兴趣的朋友们可以学习下。
    2020-02-02
  • c#获取windows桌面背景代码示例

    c#获取windows桌面背景代码示例

    这篇文章主要介绍了c#获取windows桌面背景的方法,大家参考使用吧
    2013-12-12
  • C#生成code128条形码的方法

    C#生成code128条形码的方法

    这篇文章主要介绍了C#生成code128条形码的方法,是C#程序设计中一个很实用的技巧,需要的朋友可以参考下
    2014-08-08
  • C#给PDF文件添加水印

    C#给PDF文件添加水印

    这篇文章主要为大家详细介绍了C#给PDF文件添加水印的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 解决在Unity中使用FairyGUI遇到的坑

    解决在Unity中使用FairyGUI遇到的坑

    这篇文章主要介绍了解决在Unity中使用FairyGUI遇到的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#实现随机数产生类实例

    C#实现随机数产生类实例

    这篇文章主要介绍了C#实现随机数产生类,实例分析了C#随机数的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • c#版json数据解析示例分享

    c#版json数据解析示例分享

    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式。它是基于JavaScript语法标准的一个子集。 JSON采用完全独立于语言的文本格式,可以很容易在各种网络、平台和程序之间传输。JSON的语法很简单,易于人阅读和编写,同时也易于机器解析和生成
    2014-03-03
  • 深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。
    2016-11-11

最新评论