【发布时间】:2014-12-30 22:29:48
【问题描述】:
这是我希望在页面中显示的 Customer 类及其集合。
public class Customer
{
public string Name { get; set; }
public bool Validated { get; set; }
public string Details { get; set; }
}
List<Customer> Customers = new List<Customer>()
{
new Customer() { Validated = false, Name = "Dude", Details = "some details" },
new Customer() { Validated = false, Name = "Person", Details = "some details" },
new Customer() { Validated = true, Name = "Friend", Details = "some details" },
new Customer() { Validated = false, Name = "Buddy", Details = "some details" },
};
我正在尝试在列表控件中为此数据创建数据模板。对于图像,我想根据 Validated 字段显示不同的图像。以下是我到目前为止所做的,但我不知道如何为图像设置模板。
<Page x:Class="MyTestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="500"
Title="MyTestPage" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition />
</Grid.RowDefinitions>
<ListBox x:Name="lst1" Grid.Row="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label FontFamily="Tahoma" FontSize="20" Content="Name" />
<Label FontFamily="Tahoma" FontSize="18" Content="{Binding Name}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label FontFamily="Tahoma" FontSize="14" Content="Details" />
<Label FontFamily="Tahoma" FontSize="12" Content="{Binding Details}" />
</StackPanel>
</StackPanel>
<Image Source="{Binding Image}" Height="100" Stretch="UniformToFill" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" >
<Button Content="Close" Margin="5" Width="60" Click="Close_Click" />
</StackPanel>
</Grid>
</Page>
关于如何在此数据模板中设置图像模板的任何想法?
【问题讨论】:
-
阅读 MSDN 上数据绑定概述文章的 Data Conversion 部分。
-
在阅读了几篇关于此的文章后,我创建了上面的模板,但是,在谷歌上搜索它时,似乎有很多方法可以实现这一点(有和没有转换器)。我不确定这种情况下最好的方法是什么。
-
@johnsmith 使用禁用的
ToggleButton或CheckBox和自定义Template,这将根据IsChecked的真假显示一个图像或另一个图像 -
@dkozi:你能举个例子吗?
标签: c# wpf xaml .net-4.0 datatemplate