ItemControl 中 Items的删除 上下移动 等事件操作方法

    xiaoxiao2021-12-14  18

    <ItemsControl ItemsSource="{Binding MainList}" Button.Click="StackPanel_Click"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="{Binding Name}" Width="50" Height="30"/> <Button Content="{Binding Value}" Width="50" Height="30"/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> private void StackPanel_Click(object sender, RoutedEventArgs e) { var vm = (ViewModels.MainViewModel)this.DataContext; vm.DoSomething(sender, e); } public void DoSomething(object sender, RoutedEventArgs e) { //MainList.Clear(); if (e.OriginalSource is Button) { Button bnt = (Button)e.OriginalSource; string text = bnt.Content.ToString(); for (int i = 0; i < MainList.Count; i++) { //上移 if (MainList[i].Value == text) { MainListProperty temp = new MainListProperty(); if (i > 0) { temp = MainList[i - 1]; MainList[i - 1] = MainList[i]; MainList[i] = temp; } } //删除 else if (MainList[i].Name == text) { MainList.Remove(MainList[i]); } } } }
    转载请注明原文地址: https://ju.6miu.com/read-971559.html

    最新回复(0)