<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)
{
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