我正在编写一个程序来显示与文本相关的文本和图像。数据的性质使得我可能或可能没有每个文本的图像。这是信息在对象的集合中。每个对象都有文本和图像路径。如果图像不存在则路径为空。对象的类是
public class MyInfo
{
public DateTime EntryDate { set; get; }
public string NoteText { set; get; }
public string ImagePath { set; get; }
}
我使用DataGrid来显示信息。第一列显示文本,第二列显示图像。如果没有图像,则第二列为空。这看起来不太好,客户要求更改UI,以便在没有图像时应该占用整行。他还希望在行之间有清晰的分隔符。我已经有了交替的颜色,但是文字和图像都不合适。
请建议如何增强网格。如果DataGrid不是正确的控件,那么解决它的其他控件/方法是什么。
由于
答案 0 :(得分:0)
我建议不要使用DataGrid
,而是建议ListBox
使用DataTemplate
,方式与此类似:
<ListBox ItemsSource="{Binding Path=MyInfoCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=EntryDate}" />
<TextBlock Grid.Column="1" Text="{Binding Path=NoteText}" />
<Image Grid.Column="2" Source="{Binding Path=ImagePath}" />
</Grid>
</DataTemplate>
</ListBox .ItemTemplate>
</ListBox >
MyInfoCollection
是MyInfo
个对象的ObservableCollection。
答案 1 :(得分:0)
就个人而言,我会使用ListBox并使用ListBox.ItemTemplate来定义行的外观。这将提供更大的灵活性,更好地实现您的需求。正如Ashok所说,你会想要使用值转换器将空字符串转换为&#34; Collapsed&#34;能见度选项。
转换器示例:
public class EmptyStringToCollapsedConverter : IValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = (value as string);
return String.IsNullOrWhiteSpace(s)? Visibility.Collapsed : Visibility.Visible;
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}