DataGrid从用户控件添加列表视图信息

时间:2018-08-16 03:39:11

标签: listview datagrid

我是新开发人员,正在练习,创建一个WPF程序以用于一个自助餐厅的点销售,但是,我不知道UserControl在WindowsMain XAML中如何填充Datagrid。我添加了名称x:Name =“ dgSales”,但是从用户控件中我收到此错误消息错误1名称'dgSales'在当前上下文中不存在H:\ WPF Ed \ Cafeteria \ Cafeteria \ Desayuno.xaml.cs < / p>

XAML代码主窗口

<Window x:Class="Cafeteria.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Cafeteria Chep" Height="700" Width="1200" WindowStartupLocation="CenterOwner" WindowState="Maximized">
<Grid Margin="10">
    <DataGrid Margin="0,0,700,286" Background="Bisque" ItemsSource="{Binding}" x:Name="dgSales" />
    <Grid Margin="0,369,700,78" Background="LightGray">
        <TextBlock Margin="30,10,10,153" Height="40" FontSize="20" FontWeight="Bold"><Run Text="Total"/></TextBlock>
        <TextBlock Margin="30,55,10,108" FontSize="20" FontWeight="Bold"><Run Text="Sub Total"/></TextBlock>
        <TextBlock Margin="30,100,10,63" FontSize="20" FontWeight="Bold"><Run Text="Total a Pagar"/></TextBlock>
    </Grid>
    <Border Background="Salmon" BorderBrush="Gainsboro" BorderThickness="3" Width="200" Height="650" Margin="486,0" VerticalAlignment="Top">
        <WrapPanel Margin="0,40,0,0" Orientation="Vertical" Width="100">
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnDesayunos" Content="Desayunos"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnComida" Content="Comida"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnEnsalada" Content="Ensaladas"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnSodas" Content="Sodas"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnJugos" Content="Jugos"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnDulces" Content="Dulces"/>
            <Button Height="80" FontSize="18" FontWeight="Bold" Click="BtnSabritas" Content="Sabritas"/>
        </WrapPanel>
    </Border>
    <Grid Margin="0,587,700,0" Background="LightCoral">
        <DockPanel>
            <Button DockPanel.Dock="Left" Width="212" FontWeight="Bold" FontSize="18" Content="Cobrar"/>
            <Button DockPanel.Dock="Bottom" HorizontalAlignment="Right" Width="224" FontWeight="Bold" FontSize="18" Content="Corregir"/>
        </DockPanel>
    </Grid>
    <StackPanel Margin="691,0,0,0" x:Name="stkPanel" Background="Gainsboro" Orientation="Vertical" VerticalAlignment="Stretch" />
</Grid>

public partial class Desayuno : UserControl
{
    public Desayuno()
    {
        InitializeComponent();
    }

    public void ListView_MouseDoubleClick_1(object sender, RoutedEventArgs e)
    {
     //   MessageBox.Show("You clicked me at ");
        List<ventas> venta = new List<ventas>();
        venta.Add(new ventas() { Articulo = "Huevo con Frijoles", Precio = 10, Cantidad = 1, total = 20, Comentarios = "Muy bueno" });
        dgSales.ItemsSource = venta;           

    }
}

On click on left image, listview will show at green section

0 个答案:

没有答案