我目前有一个实体(Entity
),它有一个集合属性。我想知道为什么当我尝试选择先前选择的项目时,SelectionChanged
仅会触发一次,并且不会再次触发SelectionChanged
。
MainWindowViewModel
public MainWindowViewModel()
{
var a = new List<Test>();
a.Add(new Test() { Name = "Leo", Test1 = new List<Test1> { new Test1() { Content = "aaa"} } });
a.Add(new Test() { Name = "2", Test1 = new List<Test1> { new Test1() { Content = "bbb"} } });
a.Add(new Test() { Name = "Le33o", Test1 = new List<Test1> { new Test1() { Content = "ccc"} } });
A = a;
}
private List<Test> _a;
public List<Test> A
{
get { return _a; }
set { _a = value; OnPropertyChanged("A");}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
我的主窗口
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
private void Test(object sender, SelectionChangedEventArgs e)
{
}
我的列表框结构
public class Test
{
public List<Test1> Test1 { get; set; }
public string Name
{
get;set;
}
}
public class Test1
{
public string Content { get; set; }
}
我选择第一个对象,事件触发,我选择第二个对象,事件再次触发,我重新选择第一个对象,事件不再触发,当我选择第三个对象时,事件又被触发了。似乎它只会触发和调用一次事件。
我的XAML
代码:
<ItemsControl x:Name="Lists" ItemsSource="{Binding A}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold"
Style="{StaticResource DefaultTextBlockStyle}" />
<ListBox SelectionChanged="Test" ItemsSource="{Binding Test1}"
Margin="5,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
测试方法只是一个空的方法,我只是想在每次更改时打断点。
private void Test(object sender, SelectionChangedEventArgs e)
{
}
更新1: 我尝试在一个简单的WPF应用程序中复现这个问题,似乎ListBoxItem被设置为false但我使用Snoop检查后发现所有控件都是可用的。它只是变成了一种看起来像被禁用的灰色背景。我将进一步调查。
更新2: 当你改变一个项目时,ListBoxItem的IsSelected属性没有被取消选择。