C# ListBox 绑定的问题
<Windowx:Class="TestListBox.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006...
<Window x:Class="TestListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="ML" Margin="20,20,10,10">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<ZoomableCanvas Loaded="ZoomableCanvas_Loaded" >
</ZoomableCanvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<Grid Height="{Binding Height}" Width="{Binding Width}" Canvas.Top="{Binding top }" Canvas.Left="{Binding left}"
Background="{Binding Background}"></Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="18" HorizontalAlignment="Left" Margin="28,0,0,0" Name="button1" VerticalAlignment="Top" Width="43" Click="button1_Click" />
</Grid>
</Window>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ML.ItemsSource = HH;
}
double a, b;
Grid pp = new Grid();
ZoomableCanvas ZC = new ZoomableCanvas();
ObservableCollection<object> HH = new ObservableCollection<object>();
private void ZoomableCanvas_Loaded(object sender, RoutedEventArgs e)
{
ZC = (ZoomableCanvas)sender ;
}
Grid ll = new Grid();
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
for (int j=0; j<20;j++)
{
double left = 20 + j * 70;
double top =20 + i * 90;
double Width = 60;
double Height = 80;
SolidColorBrush Background = Brushes.Red;
HH.Add(new {left,top,Width,Height,Background });
}
}
}
}
这是在加上了<ListBox.ItemTemplate>的情况下得到的
为什么只有一个Grid出现啊,而不是200个啊? 展开
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="ML" Margin="20,20,10,10">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<ZoomableCanvas Loaded="ZoomableCanvas_Loaded" >
</ZoomableCanvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<Grid Height="{Binding Height}" Width="{Binding Width}" Canvas.Top="{Binding top }" Canvas.Left="{Binding left}"
Background="{Binding Background}"></Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="18" HorizontalAlignment="Left" Margin="28,0,0,0" Name="button1" VerticalAlignment="Top" Width="43" Click="button1_Click" />
</Grid>
</Window>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ML.ItemsSource = HH;
}
double a, b;
Grid pp = new Grid();
ZoomableCanvas ZC = new ZoomableCanvas();
ObservableCollection<object> HH = new ObservableCollection<object>();
private void ZoomableCanvas_Loaded(object sender, RoutedEventArgs e)
{
ZC = (ZoomableCanvas)sender ;
}
Grid ll = new Grid();
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
for (int j=0; j<20;j++)
{
double left = 20 + j * 70;
double top =20 + i * 90;
double Width = 60;
double Height = 80;
SolidColorBrush Background = Brushes.Red;
HH.Add(new {left,top,Width,Height,Background });
}
}
}
}
这是在加上了<ListBox.ItemTemplate>的情况下得到的
为什么只有一个Grid出现啊,而不是200个啊? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询