WPF ListBox 问题

我的目的很简单,就要在ListBox中创建200个Rect(必须用到Canvas),并且能单独选中每一个Rect,问了很多人,搞了很久都没搞出来,求助:前面xaml通用的... 我的目的很简单,就要在ListBox中创建200个Rect(必须用到Canvas),并且能单独选中每一个Rect,问了很多人,搞了很久都没搞出来,求助:
前面xaml通用的,没贴上来:
<Grid>
<ListBox Name="ML" Margin="20,30,10,10" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Name="ca" Loaded="ZoomableCanvas_Loaded" >
</Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Height" Value="{Binding Height}"></Setter>
<Setter Property="Canvas.Left" Value="{Binding left}"></Setter>
<Setter Property="Canvas.Top" Value="{Binding top}"></Setter>
<Setter Property="Width" Value="{Binding Width}"></Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Rectangle Fill="Background" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Content="Button" Height="24" HorizontalAlignment="Left" Margin="28,0,0,0" Name="button1" VerticalAlignment="Top" Width="57" Click="button1_Click" />
</Grid>
</Window>

namespace TestListBox
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ML.ItemsSource = HH;
}
Canvas ZC = new Canvas();
ObservableCollection<object> HH = new ObservableCollection<object>();
private void ZoomableCanvas_Loaded(object sender, RoutedEventArgs e)
{
ZC = (Canvas)sender ;
}
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;
Thickness oo =new Thickness(left,top,0,0);
double Width = 60;
double Height = 80;
SolidColorBrush Background = Brushes.Red;
string s = i.ToString() + j.ToString();
HH.Add(new {oo,Width,Height,Background,left,top,s });
}
}
}
}
}
展开
 我来答
Mickal小米
2013-02-22 · TA获得超过3666个赞
知道大有可为答主
回答量:1542
采纳率:100%
帮助的人:1751万
展开全部
ListBox.ItemsPanel只能设置成队列式布局控件(stackpanel、wrappanel及它们的继承类)。我不清楚你为何一定要用到Canvas,能否说明下你的需求?
更多追问追答
追问
不对,ListBox.ItemsPanel可以Canvas的布局的,网上也有不少的例子,比如这里就有一个比较好的例子http://blogs.msdn.com/b/kaelr/archive/2010/08/11/zoomableapplication2-a-million-items.aspx 

我要设成Canvas,是因为我在后面的布局中要设计到大量的 点 ,线 (包括面)的精确定位绘制
追答
ZoomableCanvas是他自定义的类吧,不过我没看到源文件,WPF提供的默认canvas是实现不了的。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式