簡體   English   中英

根據WPF數據網格中的列值顯示圖像

[英]Displaying Images based on column value in WPF datagrid

我從SQL服務器表中查詢了一個數據表。
數據表只包含一列,它將包含0到9之間的數字。
我需要在WPF datagrid中顯示它。 我已經完成了普通的datagrid顯示。
但我需要在該列中顯示單獨的圖片和該特定數字的一些文本。
是否有可能通過Datagrid?

使用DataGridTemplateColumn並使用其綁定IValueConverter ,將改變你的intImageSource

這是一個小工作示例:

MainWindow.xaml

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:StackOverflow"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <local:IntToImageConverter x:Key="IntToImageConverter" />
    </Window.Resources>

    <DataGrid>

        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Converter={StaticResource IntToImageConverter}}" />
                            <TextBlock Text="Image number : " Margin="5, 0, 0, 0" />
                            <TextBlock Text="{Binding}" Margin="5, 0, 0, 0" />
                    </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>

        <sys:Int32>0</sys:Int32>
        <sys:Int32>1</sys:Int32>

    </DataGrid>

</Window>

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace StackOverflow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class IntToImageConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ImageSource result = null;
            var intValue = (int)value;
            switch (intValue)
            {
                case 0:
                    {
                        result = new BitmapImage(new Uri(@"your_path_to_image_0"));
                        break;
                    }

                case 1:
                    {
                        result = new BitmapImage(new Uri(@"your_path_to_image_1"));
                        break;
                    }
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM