以下是我想要在頁面中顯示的Customer類及其集合。基於數據模板中bool值的圖像設置模板
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" },
};
我想在列表控件中爲這些數據創建一個數據模板。對於圖像,我想顯示基於驗證字段的不同圖像。以下是我迄今爲止所做的,但我不知道如何設置圖像模板。
<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>
有關如何在此數據模板中設置圖像模板的任何想法?
閱讀的[數據轉換(http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_conversion)節MSDN上的數據綁定概述文章。 – Clemens
我在閱讀過幾篇關於這方面的文章之後創建了上面的模板,但是,當用谷歌搜索的時候,看起來好像很多方法來完成這個(有和沒有轉換器)。我不確定這種情況下最好的方法是什麼。 – BKS
@johnsmith使用禁用'ToggleButton'或'CheckBox'自定義'模板',將顯示一個圖像或另一個取決於'IsChecked'爲真或假 – dkozl