Le réseau pour les développeurs >
Forums - Accueil
>
Windows Presentation Foundation (WPF)
>
ListView DataBinding in UserControl
ListView DataBinding in UserControl
- Hi,I have two question with ListView DataBinding,Note: This is WPF User Control1.Using the code below, I am unable to display data on the WPF form. Why this method not displying data??2. Using code I am able display data on WPF form, but the Title are repating.., how to not repeate Titles ??
<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>
<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
Réponses
- 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++- Marqué comme réponseBruce.ZhouMSFT, Modérateurvendredi 10 juillet 2009 09:02
Toutes les réponses
- 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++- Marqué comme réponseBruce.ZhouMSFT, Modérateurvendredi 10 juillet 2009 09:02
- 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.

