Подскажите как в Xaml, через DataContext или ItemSource(или я не правильно думаю, что бы прибиндить из XAML свою коллекцию _ListProduct. (Желательно с тремя столбиками, но то уже не столь важно.)
Из Кода делал вот так, но мне нужно из Xaml привязатся:
LBs.ItemsSource = ListProducts._ListProducts;
У меня есть класс Product
public class Product
{
private string name;
private double cost;
private double weight;
public string Name
{
get => name;
set => name = value;
}
public double Cost
{
get => cost;
set => cost = value;
}
public double Weight
{
get => weight;
set => weight = value;
}
public override string ToString()
{
return $@"{Name} {Cost} {Weight}";
}
}
Есть класс ListProduct
class ListProducts
{
private static readonly List<Product> _products = new List<Product>
{
new Product{Name = "Banana", Cost = 10.55, Weight = 233.8},
new Product{Name = "Potatos", Cost = 17.85, Weight = 70.8},
new Product{Name = "Qiwi", Cost = 19.30, Weight = 90.8},
};
public static List<Product> _ListProducts
{
get => _products;
}
}
Я Пробовал вот так:
<Window x:Class="ListProduct.MainWindow"
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"
xmlns:local="clr-namespace:ListProduct"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListView Name="LBs" Width="300" Height="450"/>
</Grid>
Вот так получилось реализовать
<Window x:Class="ListProduct.MainWindow"
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"
xmlns:local="clr-namespace:ListProduct"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ListProducts/>
</Window.DataContext>
<Grid>
<ListView
x:Name="listView"
HorizontalAlignment="Left"
Height="401"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="773"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding _ListProducts}" SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Path=Name}">
<GridViewColumnHeader HorizontalAlignment="Center" Tag="Cost" Click="ButtonSort_OnClick">Название</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Path=Cost}" >
<GridViewColumnHeader HorizontalAlignment="Center" Tag="Cost" Click="ButtonSort_OnClick" >Цена</GridViewColumnHeader>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding Path=Weight}">
<GridViewColumnHeader HorizontalAlignment="Center" Tag="Weight" Click="ButtonSort_OnClick">Вес</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости