我有一个列表视图,该列表为我提供了参加活动的所有人员的列表。在组合框中选择了该事件,并且listview应该从数据库的List <>中吸引参与者。我想每次选择更改时都执行此操作。我必须警告你,我对此很陌生。但是我已经从互联网上尝试了很多示例和代码,但是它们只是行不通..我使用以下代码:
C#
private void cbEvenement_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// this is he eventID extracted from the list by using the Selected index
evid = _evenementen[cbEvenement.SelectedIndex].EvenementID;
//I give the EventID to the Method in my database connector
//to get the right participants from the right event.
aanmeldingen = Connect.EvenementAanmeldingen(evid);
// getting everything ready for the listview, i have a constructor class
foreach (var aanmelding in aanmeldingen)
{
aanmeldingclass.Add(new AanmeldingEvenement()
{
Voornaam = aanmelding.voornaam,
Achternaam = aanmelding.achternaam
});
}
// setting the itemsource
LVAanmeldingen.ItemsSource = aanmeldingclass;
// saw this on the internet. But it is not working?
CollectionViewSource.GetDefaultView(aanmeldingclass).Refresh();
}
Xaml
<ListView x:Name="LVAanmeldingen" HorizontalAlignment="Left" Height="458" Margin="3,67,0,0" VerticalAlignment="Top" Width="268" ItemsSource="{Binding AanmeldingCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Voornaam" Width="auto" DisplayMemberBinding="{Binding Voornaam}" ></GridViewColumn>
<GridViewColumn Header="Achternaam" Width="auto" DisplayMemberBinding="{Binding Achternaam}" ></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
班级
class AanmeldingEvenement
{
public string Voornaam { get; set; }
public string Achternaam { get; set; }
}
在互联网上找到:
private ObservableCollection<AanmeldingEvenement> Aanmeldingcollection;
public ObservableCollection<AanmeldingEvenement> AanmeldingCollection
{
get
{
if (Aanmeldingcollection == null)
{
Aanmeldingcollection = new ObservableCollection<AanmeldingEvenement>();
}
return Aanmeldingcollection;
}
set
{
Aanmeldingcollection = value;
}
}
我真的被困在这里,我想要的只是Listview,向我展示参与者并找到与数据库中的事件相关的合适参与者
答案 0 :(得分:0)
据我所知,您正在像使用WinfForms
一样进行编码。对于WPF,我强烈建议您使用MVVM
模式和DataBinding
。否则,您会丢失太多东西,无法使WPF值得。
这是我的答案(请问是否需要):
我建立了两个班来管理事件和人民。我决定将People
注册到其他Events
,但这可能是相反的,也可能是其他解决方案:
public class People
{
public string firstName { get; set; }
public string lastName { get; set; }
public List<Event> registeredEvents { get; set; }
public People()
{
registeredEvents = new List<Event>();
}
}
public class Event
{
public string name { get; set; }
public string anyInfo { get; set; }
}
然后我有一个EventManager
,它将收集信息并执行逻辑。当然,您必须根据数据库和程序的其他部分对其进行修改:
public class EventManager : INotifyPropertyChanged
{
public List<Event> events { get; set; }
public List<People> peoples { get; set; }
//this is the current event that correspond to the selected event in your combobox
private Event _currentEvent;
public Event currentEvent
{
get
{
return _currentEvent;
}
set
{
if (_currentEvent != value)
{
_currentEvent = value;
//when you change the selected event, you have to update the list of participants
OnPropertyChanged("participants");
}
}
}
public List<People> participants
{
get
{
//Here is the code to retrieve the people that registered to the selected event
return peoples.Where(p => p.registeredEvents.Contains(currentEvent)).ToList<People>();
}
}
public EventManager()
{
events = new List<Event>();
peoples = new List<People>();
}
//The following lines are specific to WPF and DataBinding
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Here you can find more information about Databinding
然后是XAML。它仅依赖于DataContext:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ComboBox ItemsSource="{Binding events}" SelectedItem="{Binding currentEvent}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ListView Grid.Column="1" ItemsSource="{Binding participants}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding firstName}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding lastName}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
最后,这是我添加Data
的代码。当然,您的角色应该有所不同。重要的是Datacontext
和我真的不在乎ComboBox
或ListView
的事实。
public MainWindow()
{
InitializeComponent();
EventManager eventManager = new EventManager();
Event wedding = new Event() { name = "Wedding" };
Event meeting = new Event() { name = "Meeting with LunaStar" };
Event skiSession = new Event() { name = "Skiing in Oklahoma" };
People bob = new People() { firstName = "Bob", lastName = "Raz." };
People irina = new People() { firstName = "Irina", lastName = "Politov." };
People kumamoto = new People() { firstName = "Kumamoto", lastName = "San" };
bob.registeredEvents.Add(wedding);
bob.registeredEvents.Add(meeting);
bob.registeredEvents.Add(skiSession);
irina.registeredEvents.Add(wedding);
irina.registeredEvents.Add(meeting);
kumamoto.registeredEvents.Add(meeting);
kumamoto.registeredEvents.Add(skiSession);
eventManager.peoples = new List<People>() { bob, irina, kumamoto };
eventManager.events = new List<Event>() { wedding, meeting, skiSession };
DataContext = eventManager;
}