День добрый,
не получается сделать выпадающий список, в качестве одной из колонок ListView.
Что есть:
- таблица - источник данных для ListView;
- одна из колонок должна быть выпадающим списком.
Ниже пример. Колонка называется BrandGroup, значений у нее может быть два: "Foreign brands" либо "Russian brands".
Разметка для окошка с ListView
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="461.2" Width="680.2">
<Grid>
<ListView
ItemsSource="{Binding Path=TableWithData}"
Name="ListView_Brands"
SelectionMode="Single"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="600" >
<ListView.View>
<GridView>
<GridViewColumn Header="Название бренда" DisplayMemberBinding="{Binding Path=BrandName}" Width="150" />
<GridViewColumn Header="Страна" DisplayMemberBinding="{Binding Path=BrandCountry}" Width="100" />
<!--вот эта колонка должна быть с выпадающим списком-->
<GridViewColumn Header="Группа" DisplayMemberBinding="{Binding Path=BrandGroup}" Width="200" />
<!--попытка создать выпадающий список-->
<GridViewColumn Header="Группа 2" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="160" SelectedValue="{Binding Path=BrandGroup}">
<ComboBoxItem Content="Russian brands" />
<ComboBoxItem Content="Foreign brands" />
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
C# код для создания источника данных
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
using System.Collections.ObjectModel;
using System.Windows;
using System.ComponentModel;
namespace Test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Ставим источник данных для формы. Сам класс ViewModel чуть ниже.
this.DataContext = new ViewModel();
}
}
/// <summary>
/// Источник данных для формы
/// </summary>
class ViewModel : ViewModelBase
{
public ObservableCollection<ListViewRow> _Source;
public ViewModel()
{
_Source = new ObservableCollection<ListViewRow>();
_Source.Add(new ListViewRow { BrandName = "Dodge", BrandCountry = "USA", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "FAW", BrandCountry = "China", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "Ferrari", BrandCountry = "Italy", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "Fiat", BrandCountry = "Italy", BrandGroup = "Foreign brands" });
_Source.Add(new ListViewRow { BrandName = "VAZ", BrandCountry = "Russia", BrandGroup = "Russian brands" });
_Source.Add(new ListViewRow { BrandName = "Kamaz", BrandCountry = "Russia", BrandGroup = "Russian brands" });
}
/// <summary>
/// Свойство - будет ItemsSource для ListView
/// </summary>
public ObservableCollection<ListViewRow> TableWithData
{
get { return _Source; }
}
}
/// <summary>
/// Одна строка с данными
/// </summary>
public class ListViewRow
{
public string BrandName { get; set; }
public string BrandCountry { get; set; }
public string BrandGroup { get; set; }
}
/// <summary>
/// Вспомогательный класс для создания всех ViewModel
/// </summary>
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Картинка с ожидаемым результатом ниже. Если надо могу выложить тестовый проект (Visual Studio 2013) со всей этой разметкой.
Спасибо.