MSDN > フォーラム ホーム > Windows Presentation Foundation (WPF) > ListView DataBinding in UserControl
質問する質問する
 

回答済みListView DataBinding in UserControl

  • 2009年7月4日 17:47Venkat Ganesh ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    Hi,

    I have two question with ListView DataBinding, 

    Note: This is WPF User Control

    1.Using the code below, I am unable to display data on the WPF form. Why this method not displying data??

    <ListView Grid.Row="6" Grid.ColumnSpan="2" x:Name="lvProducts" SelectionMode="Single" ItemsSource="{Binding}" DisplayMemberPath="ProductID" IsSynchronizedWithCurrentItem="True">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Product ID" DisplayMemberBinding="{Binding Path =ProductID}"></GridViewColumn>
                        <GridViewColumn Header="Product Name" DisplayMemberBinding="{Binding Path =ProductName}"></GridViewColumn>
                        <GridViewColumn Header="Product Description" DisplayMemberBinding="{Binding Path =ProductDescription}"></GridViewColumn>
                        <GridViewColumn Header="Product Price" DisplayMemberBinding="{Binding Path =ProductPrice}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
    
    2. Using code I am able display data on WPF form, but the Title are repating.., how to not repeate Titles ??

            <ListView Name="lvProducts" Grid.Row="6" Grid.ColumnSpan="2" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" BorderThickness="2" BorderBrush="Black" Height="Auto">
    
                <ListView.ItemTemplate>
                    
                    <DataTemplate>
                        <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Row="0" Grid.Column="0">ID</Label>
                                <Label Grid.Row="0" Grid.Column="1">Name</Label>
                                <Label Grid.Row="0" Grid.Column="2">Price</Label>
                                <Label Grid.Row="0" Grid.Column="3">Description</Label>
                                <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path=ProductID}"></TextBlock>
                                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=ProductName}"></TextBlock>
                                <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Path=ProductPrice}"></TextBlock>
                                <TextBlock Grid.Row="1" Grid.Column="3" Text="{Binding Path=ProductDescription}"></TextBlock>
                            </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    


    for both the ways I am using code behind below..
            private void UserControl1_Loaded(Object sender, RoutedEventArgs e)
            {
                ProductsDataContext db = new ProductsDataContext();
    
                var query = from prod in db.Products
                            select new
                            {
                                prod.ProductName,
                                prod.ProductID,
                                prod.ProductPrice,
                                prod.ProductDescription
                            };
    
                this.lvProducts.ItemsSource = query;
            }
    
    Note: This is WPF User Control

回答

  • 2009年7月4日 20:47Mark SalsberyMVPユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    I think if you remove the "DisplayMemberPath="ProductID"" from the first version, your data should display correctly.

    In the second version, you've put the titles in the item template, so every item gets title labels.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++

すべての返信

  • 2009年7月4日 20:47Mark SalsberyMVPユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    I think if you remove the "DisplayMemberPath="ProductID"" from the first version, your data should display correctly.

    In the second version, you've put the titles in the item template, so every item gets title labels.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • 2009年7月10日 9:00Bruce.ZhouMSFT, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Hi VenKat,

    I performed a test with the first version of your code, the data can display correctly. Please check out if the linq to sql query returns the correct data on your side.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.