我ListView
用来显示Button
s 的列表。在使用StackPanel
orWrapPanel
时ItemsPanel
,ListView
一切正常。当我只尝试Canvas
集合中的最后一个按钮正在工作时,其他人甚至不会在鼠标悬停时改变他们的背景。我可以让它们都正常工作吗?
<ListView ItemsSource="{Binding collection1}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Button Margin="{Binding margin}" Width="40" Height="40" Click=button_click />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
后面的代码:
public class object1
{
public double x0 { get; set; }
public double y0 { get; set; }
public Thickness margin { get { return new Thickness(x0, y0, 0, 0); } }
public object1(double x, double y)
{
x0= x;
y0 = y;
}
}
public MainWindow : Window
{
public ObservableCollection<object1> collection1 {get;set;}
public MainWindow()
{
InitializeComponent();
DataContext = this;
collection1=new ObservableCollection<object1>();
collection1.Add(new object1(20,20));
collection1.Add(new object1(20,80));
collection1.Add(new object1(80,20));
collection1.Add(new object1(80,80)); // ONLY THAT ONE WORKS
}
}