How to display values ​​as images in a GridViewColumn?

I have a GridViewColumn that I bound like this:

<GridViewColumn Header="Validated" DisplayMemberBinding="{Binding Path=Validated, Converter={StaticResource imageConverter}}" />

Binding Path = Validated returns an Enumerated value, imageConverter accepts this value and returns System.Windows.Media.Imaging.BitmapImage. I checked the value of the object referenced when one of these BitmapImage objects was created, and it appears to contain an image with the correct size.

Now my problem is that what is displayed in the GridView is the URI of the bitmap (like text), and not the image itself.

What am I doing wrong this time?

+3
source share
2 answers

imageConverter, uri .

<GridViewColumn Header="Validated">
  <GridViewColumn.CellTemplate>
    <DataTemplate>
        <Image Source="{Binding Path=Validated, Converter={StaticResource imageConverter}}" />
    </DataTemplate>
  </GridViewColumn.CellTemplate>
</GridViewColumn>
+12

: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/96f18c2b-cade-42d9-b544-c64a7ce3d82b

, , , .

public class VideoGame
{
  public string Name
  {
    get;
    set;
  }

  public string Image
  {
    get;
    set;
  }
}

-, ObservableCollection.

public partial class Window1 : Window
{
  private ObservableCollection<VideoGame> _games =
    new ObservableCollection<VideoGame>();

  public ObservableCollection<VideoGame> Games
  {
    get { return _games; }
  }

  public Window1()
  {
    _games.Add(new VideoGame() {
      Name = "Crysis",
      Image = @"C:\Crysis_Boxart_Final.jpg" });
    _games.Add(new VideoGame() {
      Name = "Unreal Tournament 3",
      Image = @"C:\Gearsofwar.JPG" });
    _games.Add(new VideoGame() {
      Name = "Gears of War",
      Image = @"C:\Crysis_Boxart_Final.jpg" });

    InitializeComponent();
  }
}

-, DataTemplate GridViewColumn.CellTemplate.

<Window x:Class="VerticalAlignSnippet.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Window1" Height="512" Width="512" Name="myWindow">
  <Grid>
    <ListView Name="myListView"
             ItemsSource="{Binding ElementName=myWindow, Path=Games}">
      <ListView.View>
        <GridView>
          <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Name}" />
          <GridViewColumn Header="Image">
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <Grid>
                  <Image Source="{Binding Image}" />
                </Grid>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</Window>

XAML. XamlReader DataTemplate .

  string str = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><Grid><Image Source=\"{x:Null}\" /></Grid></DataTemplate>";


    DataTemplate template = new DataTemplate();

    template = XamlReader.Parse(str) as DataTemplate;
    .....
    gv1.Columns[3].CellTemplate = template;
+6

Source: https://habr.com/ru/post/1767195/


All Articles