我有一个列表框,它使用 ItemTemplate 来显示图像。我希望能够更改 ItemTemplate 中显示图像的大小。通过数据绑定(bind),我可以更改宽度,但我可以看到如何执行此操作的唯一方法是将属性(例如 ImageSize)添加到我绑定(bind)到的类,然后更改集合中的每个项目以具有新的 ImageSize。是否无法访问该数据模板中项目的属性?
例如
<navigation:Page.Resources>
<DataTemplate x:Key="ListBoxItemTemplate">
<Viewbox Height="100" Width="100">
<Image Source="{Binding Image}"/>
</Viewbox>
</DataTemplate>
</navigation:Page.Resources>
<Grid>
<ListBox ItemTemplate="{StaticResource ListBoxItemTemplate}" ItemSource="{Binding Collection}"/>
</Grid>
是否可以在不将属性绑定(bind)到集合中的每个元素的情况下设置 View 框的宽度和高度?
最佳答案
您可以使用元素绑定(bind)到此。尝试这样的事情:
<UserControl.Resources>
<DataTemplate x:Key="ListBoxItemTemplate">
<Viewbox Height="{Binding Value, ElementName=slider1}"
Width="{Binding Value, ElementName=slider1}">
<Image Source="{Binding Image}"/>
</Viewbox>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="205*" />
<RowDefinition Height="95*" />
</Grid.RowDefinitions>
<ListBox ItemTemplate="{StaticResource ListBoxItemTemplate}"
ItemSource="{Binding Collection}"/>
<Slider x:Name="slider1" Value="100" Maximum="250" Grid.Row="1"/>
</Grid>
关于silverlight - 有没有办法以编程方式更改 Silverlight 中的数据/项目模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647968/