8,757
社区成员
发帖
与我相关
我的任务
分享<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:BoolToPictureConverter x:Key="boolToPic"/>
<DataTemplate x:Key="myListItemViewTemplate">
<Grid Margin="2">
<StackPanel Orientation="Horizontal">
<Image Height="60" Stretch="Fill" Source="{Binding IsWhat, Converter={StaticResource boolToPic}}"/>
<TextBox FontSize="15" Margin="5,0,0,0" FontWeight="Bold" Text="{Binding SomeThing}"/>
</StackPanel>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Height="201" HorizontalAlignment="Left" Margin="29,29,0,0" Name="listBox1" VerticalAlignment="Top" Width="214" ItemTemplate="{StaticResource myListItemViewTemplate}" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
namespace WpfTest
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<TestClass> list = new List<TestClass>();
list.Add(new TestClass { IsWhat = true, SomeThing = "真" });
list.Add(new TestClass { IsWhat = false, SomeThing = "假" });
list.Add(new TestClass { IsWhat = false, SomeThing = "假" });
list.Add(new TestClass { IsWhat = true, SomeThing = "真" });
listBox1.ItemsSource = list;
}
}
public class TestClass
{
public Boolean IsWhat { get; set; }
public String SomeThing { get; set; }
}
//转换器
public class BoolToPictureConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Boolean isBool = (Boolean)value;
if (isBool) {
return new BitmapImage(new Uri(@"/Images/Bluehills.jpg", UriKind.RelativeOrAbsolute));
}
return new BitmapImage(new Uri(@"/Images/Sunset.jpg", UriKind.RelativeOrAbsolute));
}
//未被用到
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
