gpt4 book ai didi

xaml - 在数据模板中更改图像的宽度和高度

转载 作者:行者123 更新时间:2023-12-01 06:09:33 26 4
gpt4 key购买 nike

我在数据模板中有一张图片。我想根据某些条件改变它的高度和宽度。我怎样才能做到这一点?

我拥有的 XAML 代码:

    <ListBox x:Name="options_stack" HorizontalAlignment="Left" Margin="198,569,0,33" Width="603" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Auto" Height="123" Style="{StaticResource ListBoxStyle}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image x:Name="options_image" Source ="{Binding}" Stretch="Fill" Width="123" Height="123" MaxHeight="123" MaxWidth="123" Tapped="apply_accessory"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

最佳答案

选项 1

由于您要绑定(bind)源属性,我会创建一个数据结构(用作 DataContext)来保存 SourceWidthHeight 属性然后绑定(bind)它们:

<Image x:Name="options_image"
Source ="{Binding Source}"
Width="{Binding Width}" Height="{Binding Height}"
MaxWidth="{Binding Width}" MaxHeight="{Binding Height}"
Stretch="Fill" Tapped="apply_accessory"/>

选项 2

另一种选择是创建不同的DataTemplates 和一个DataTemplateSelector 以在满足特定条件时应用。

DataTemplateSelector 引用:http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

选项 3

如果您不喜欢以上两个选项,您还有另一种方法。在我的上一份工作中(Taskin,如果你想看一下,你可以在我的个人资料中看到链接)我需要为不同的 ListViewItems 显示不同的颜色。我不想在我的 Task 模型中创建一个新属性来保存颜色或为此创建一个 TemplateSelector。所以我创建了一个简单的 IValueConverter,它使用一个已经存在的 Priority 对象属性并返回它的颜色。然后我像这样绑定(bind)它:

<Border Background="{Binding Priority, Converter={StaticResource priorityToColorConverter}}"/>

XAML 为您提供了许多方法来实现您想要的,您可以选择最佳和最简洁的方法来解决您面临的问题。

关于xaml - 在数据模板中更改图像的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16300252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com