【问题标题】:Setup template for image based on bool value within a Data Template基于数据模板中的布尔值的图像设置模板
【发布时间】: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 使用禁用的ToggleButtonCheckBox 和自定义Template,这将根据IsChecked 的真假显示一个图像或另一个图像
  • @dkozi:你能举个例子吗?

标签: c# wpf xaml .net-4.0 datatemplate


【解决方案1】:

创建一个实现IValueConverter接口的自定义转换器,它根据绑定值返回想要的BitmapImage,你可能只需要为Convert方法创建逻辑,这样你就可以离开ConvertBack方法原样

像这样将你的转换器添加到资源中

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

并将其添加到图像的绑定中

您的 xaml 图像将如下所示:

&lt;Image Source="{Binding Validated, Converter={StaticResource MyCustomValueConverter}" Height="100" Stretch="UniformToFill"/&gt;

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 2015-05-14
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2014-03-23
    • 2017-05-15
    • 2021-02-03
    • 2020-04-20
    相关资源
    最近更新 更多