请问WPF Xaml 用ItemTemplate和用ItemContainerStyle的区别,谁能解释一下以下现象:
后台代码for(inti=0;i<10;i++){for(intj=0;j<20;j++){doubleleft=20+j*80;doubletop=20+i*90;Th...
后台代码
for (int i = 0; i < 10; i++)
{
for (int j=0; j<20;j++)
{
double left = 20 + j * 80;
double top =20 + i * 90;
Thickness oo =new Thickness(left,top,0,0);
double width = 60;
double height = 80;
SolidColorBrush bb = Brushes.Blue;
Rectangle uu = new Rectangle();
uu.Fill = Brushes.Blue;
double theigth = 60;
BitmapImage aa = new BitmapImage();
aa.BeginInit();
aa.UriSource = new Uri(@"e:\dm2\work\0206_IMG_OUT_x.jpg");
aa.EndInit();
Image yy = new Image();
yy.Source = aa;
string s = i.ToString() + j.ToString();
HH.Add(new { oo, width, height, bb, left, top, theigth,aa,s });/*有些东西是用不到的,可以不理它*/
}
}
xaml中部分代码,方法1:
<Window.Resources >
<DataTemplate x:Key="qq">
<Image Margin="{Binding oo}" Source="{Binding aa}" Height="{Binding heigth}"
Width="{Binding width}" Stretch="Fill"></Image>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="ML" Margin="20,30,10,10" ItemTemplate="{StaticResource qq}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Name="ca" Loaded="ZoomableCanvas_Loaded" >
</Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
方法2:
<ListBox Name="ML" Margin="20,30,10,10" >
<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="Background" Value="{Binding bb}"></Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
<Image Source="{Binding aa}" Height="{Binding heigth}"
Width="{Binding width}" Stretch="Fill"></Image>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
两种出来的效果完全不一样:
方法1出来的不能单独选中每个image,方法2可以,why?
上图是第二种方法的,第三张图片可以被单独选中,但第一种方法,如果选则的话,则所有的图片都被选中了,还不止于此,应该是选中的整个canvas,求解释 展开
for (int i = 0; i < 10; i++)
{
for (int j=0; j<20;j++)
{
double left = 20 + j * 80;
double top =20 + i * 90;
Thickness oo =new Thickness(left,top,0,0);
double width = 60;
double height = 80;
SolidColorBrush bb = Brushes.Blue;
Rectangle uu = new Rectangle();
uu.Fill = Brushes.Blue;
double theigth = 60;
BitmapImage aa = new BitmapImage();
aa.BeginInit();
aa.UriSource = new Uri(@"e:\dm2\work\0206_IMG_OUT_x.jpg");
aa.EndInit();
Image yy = new Image();
yy.Source = aa;
string s = i.ToString() + j.ToString();
HH.Add(new { oo, width, height, bb, left, top, theigth,aa,s });/*有些东西是用不到的,可以不理它*/
}
}
xaml中部分代码,方法1:
<Window.Resources >
<DataTemplate x:Key="qq">
<Image Margin="{Binding oo}" Source="{Binding aa}" Height="{Binding heigth}"
Width="{Binding width}" Stretch="Fill"></Image>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="ML" Margin="20,30,10,10" ItemTemplate="{StaticResource qq}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Name="ca" Loaded="ZoomableCanvas_Loaded" >
</Canvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
方法2:
<ListBox Name="ML" Margin="20,30,10,10" >
<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="Background" Value="{Binding bb}"></Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate >
<Image Source="{Binding aa}" Height="{Binding heigth}"
Width="{Binding width}" Stretch="Fill"></Image>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
两种出来的效果完全不一样:
方法1出来的不能单独选中每个image,方法2可以,why?
上图是第二种方法的,第三张图片可以被单独选中,但第一种方法,如果选则的话,则所有的图片都被选中了,还不止于此,应该是选中的整个canvas,求解释 展开
2个回答
展开全部
估计你的模板绑定到同一个属性上了
追问
代码都在,能对照代码说具体一点吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询