2014-12-30 56 views
0

以下是我想要在頁面中顯示的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> 

有關如何在此數據模板中設置圖像模板的任何想法?

+0

閱讀的[數據轉換(http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_conversion)節MSDN上的數據綁定概述文章。 – Clemens

+0

我在閱讀過幾篇關於這方面的文章之後創建了上面的模板,但是,當用谷歌搜索的時候,看起來好像很多方法來完成這個(有和沒有轉換器)。我不確定這種情況下最好的方法是什麼。 – BKS

+0

@johnsmith使用禁用'ToggleButton'或'CheckBox'自定義'模板',將顯示一個圖像或另一個取決於'IsChecked'爲真或假 – dkozl

回答

1

創建一個自定義轉換器實現IValueConverter接口,它返回一個基於綁定值要BitmapImage,你可能只需要創建爲Convert方法的邏輯,所以,你可以離開ConvertBack方法是

加您轉換器的資源,像這樣

<Page.Resources> <myConverters:MyValueConverter x:Key="MyCustomValueConverter"/> <!-- dont forget to add the xmlns to the page --> </page.Resources>

,並把它添加到您的圖像

的結合

XAML中的圖像將是這個樣子:

<Image Source="{Binding Validated, Converter={StaticResource MyCustomValueConverter}" Height="100" Stretch="UniformToFill"/>