我目前有Entity
一个集合属性。我想知道为什么只有一次会触发并且一旦我尝试选择之前选择的项目SelectionChanged
就不会再次触发。SelectionChanged
主窗口视图模型
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 正在将 IsEnabled 设置为 false,但我窥探了它并启用了所有控件。它只是得到一个看起来像禁用的灰色背景。将尝试进一步调查。
更新 2:当您更改项目时,似乎没有取消设置 ListBoxItem IsSelected 属性。