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