这是我的问题,我希望有一些ListViews,其ItemsSource附加了与其他ListViews的SelectedItem的绑定。
所以我有一个“Tours”ListView,它的ItemSource与ObservableCollection属性绑定。
现在我希望“派对”ListView与“Tours”ListView的SelectedItem绑定。
之后,我希望“Équipes”ListView与“Parties”ListView的SelectedItem绑定。
等等......
现在,它正在运行,但程序崩溃了:
我认为当我改变“游览”时,“Équipe”指向不合适的东西。
这是ListViews的XAML和内容(内容现在没有绑定):
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel>
<Label Style="{StaticResource menu}" Content="Tours" />
<ListView Name="lvTours" ItemsSource="{Binding Tours}" SelectionChanged="lvTours_SelectionChanged">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
<Label Style="{StaticResource menu}" Content="Parties" />
<ListView Name="lvParties" DataContext="{Binding RelativeSource={RelativeSource Self}}" SelectionChanged="lvParties_SelectionChanged" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
<Label Style="{StaticResource menu}" Content="Équipes" />
<ListView Name="lvEquipes" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
</StackPanel>
<Label Content="Here's the content..." Grid.Column="1" Margin="30" />
</Grid>
这是cs:
using Lama.Logic.Model.Test;
using System.Windows.Controls;
namespace Lama.UI.UC.TournoiControls.StatistiquesControls
{
/// <summary>
/// Interaction logic for Statistiques.xaml
/// </summary>
public partial class StatistiquesUC : UserControl
{
public Tour SelectedTour { get; set; }
public Partie SelectedPartie { get; set; }
public StatistiquesUC()
{
InitializeComponent();
}
private void lvTours_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
lvParties.ItemsSource = ((Tour)lvTours.SelectedItem).Parties;
}
private void lvParties_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
lvEquipes.ItemsSource = ((Partie)lvParties.SelectedItem).Equipes;
}
}
}
所以:
谢谢,对不起我的英文!
答案 0 :(得分:1)
由于你不想要一个ViewModel,这是另一个approche:
在你的xaml中不需要SelectionChanged,只需绑定Lisview Equipe&amp;的itemSource。分享父列表视图如下:
<Grid DataContext="{Binding YourViewModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel>
<Label Style="{StaticResource menu}" Content="Tours" />
<ListView Name="lvTours" ItemsSource="{Binding Tours}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
<Label Style="{StaticResource menu}" Content="Parties" />
<ListView Name="lvParties" ItemsSource="{Binding ElementName=lvTours, Path=SelectedItem.Parties}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
<Label Style="{StaticResource menu}" Content="Équipes" />
<ListView Name="lvEquipes" ItemsSource="{Binding ElementName=lvParties, Path=SelectedItem.Equipes}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Nom}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
</StackPanel>
<Label Content="Here's the content..." Grid.Column="1" Margin="30" />
</Grid>
希望有所帮助