本文介绍了列表框和数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从列表框中选择时,它应该显示在数据网格中.
没有得到,请帮助我.
当我保留dg.itemsource或dg.datacontext时,我已经尝试过了.

这是代码

When I am selecting from listbox it should display in data grid.
It is not getting, please help me.
When I kept dg.itemsource or dg.datacontext it is not coming I have tried.

Here is the code

<navigation:Page xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="NorthSilver.Drop"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

           mc:Ignorable="d"

           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

           d:DesignWidth="640" d:DesignHeight="480"

           Title="Drop Page" Loaded="Page_Loaded" >
    <navigation:Page.Resources>
        <DataTemplate x:Key="Cattemplate">
            <StackPanel Margin="0,0,0,5" Orientation="Horizontal">
                <TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Text="{Binding Path=CategoryID}"/>
                <TextBlock Margin="10,0,0,0" VerticalAlignment="Center" Text="{Binding Path=CategoryName}"/>
            </StackPanel>
        </DataTemplate>
    </navigation:Page.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="120"></RowDefinition>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <sdk:DataGrid Name="DG" Margin="12,0,12,0" />
        <ListBox Grid.Row="0" Grid.Column="1" Name="lstcategores" Margin="10,0,10,0" SelectionChanged="lstcategores_SelectionChanged" ItemTemplate="{StaticResource Cattemplate}"/>
    </Grid>
</navigation:Page>



xaml.cs



xaml.cs

 private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            prod = new ProductServiceReference.ProductServiceClient();
            prod.GetProductsByCategoryCompleted+= prodservice_GetProductscompleted;
            prod.GetCategoriesCompleted += prodservice_GetCategoriescompleted;
            prod.GetProductsAsync();
            prod.GetCategoriesAsync();
        }

        private void prodservice_GetCategoriescompleted(object sender, ProductServiceReference.GetCategoriesCompletedEventArgs e)
        {
            lstcategores.ItemsSource = e.Result;
        }



        private void lstcategores_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int CategoryID = ((ProductServiceReference.Categories)((ListBox)sender).SelectedItem).CategoryID;
            prod.GetProductsByCategoryAsync(CategoryID);

        }

        private void prodservice_GetProductscompleted(object sender, ProductServiceReference.GetProductsByCategoryCompletedEventArgs e)
        {
            DG.DataContext = e.Result;

        }

in Services
public bool GetProductsByCategory(int categoryid)
        {
            NorthwindClassesDataContext db = new NorthwindClassesDataContext();
            List<Products> prod=new List<Products>();
            try
            {
                prod = (from p in db.Products
                            where p.CategoryID == categoryid
                            select new Products { ProductID = p.ProductID, ProductName = p.ProductName }).ToList();
            }
            catch (Exception)
            {
                throw new FaultException("no Categories are defined");
            }
            return true;
        }

推荐答案


这篇关于列表框和数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:36