C# 显示图像(如果存在于datagrid中)

C# 显示图像(如果存在于datagrid中),c#,wpf,image,datagrid,C#,Wpf,Image,Datagrid,我正在写一个程序来显示与文本相关的文本和图像。数据的本质是,我可能会也可能不会对每一个文本都有图像。这是对象集合中的信息。每个对象都有文本和图像路径。若图像不在那个里,那个么路径是空的。对象的类是 public class MyInfo { public DateTime EntryDate { set; get; } public string NoteText { set; get; } public string ImagePath { set; get; } }

我正在写一个程序来显示与文本相关的文本和图像。数据的本质是,我可能会也可能不会对每一个文本都有图像。这是对象集合中的信息。每个对象都有文本和图像路径。若图像不在那个里,那个么路径是空的。对象的类是

public class MyInfo
{
    public DateTime EntryDate { set; get; }
    public string NoteText { set; get; }
    public string ImagePath { set; get; }
}
我使用DataGrid来显示信息。第一列显示文本,第二列显示图像。如果没有图像,则第二列为空。这看起来不太好,客户要求更改UI,以便在没有图像的情况下占据整行。他还希望在行之间有清晰的分隔符。我已经有了交替的颜色,但和文本和图像都不协调

请建议如何增强网格。如果DataGrid不是正确的控件,那么解决它的其他控件/方法是什么


谢谢

不要使用
数据网格
,我建议将
列表框
数据模板
一起使用,方式与此类似:

<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
对象的可观察集合。

我个人会创建一个列表框,并使用ListBox.ItemTemplate定义行的外观。这将提供更大的灵活性,更好地实现您想要的。正如Ashok所说,您需要使用值转换器将空字符串转换为“折叠”可见性选项

转换器示例:

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();
    }
}

您可以为该特定列添加值转换器,并根据值true或false设置列的可见性。从来没有想过转换器。