将图像列表作为数据网格中的列插入

时间:2017-03-10 13:24:02

标签: c# wpf image list datagrid

我制作了几个列表,看起来像这样:

public class MyDataObject
{
    public double AmountNeed { get; set; }
    public double TotalLose { get; set; }
    public double TotalGain { get; set; }
    public double TotalCost { get; set; }
}

经过一些计算后,我将它们插入dataGrid

var L = new List<MyDataObject>();
for (int z = 0; z < list_Exp.Count; z++)
    {

       var d = new MyDataObject();

       d.AmountNeed = Math.Ceiling((goalexp - currentexp) / (list_Exp[z]));
       d.TotalLose = d.AmountNeed * (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]);
       d.TotalGain = d.AmountNeed * list_AmountMade[z] * list_SellPrice[z];
       d.TotalCost = d.TotalGain - d.TotalLose;

       L.Add(d);

    }

dataGrid.ItemsSource = L;

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" HorizontalAlignment="Left" Height="70" Margin="10,10,0,0" VerticalAlignment="Top" Width="125" Click="button_Click"/>
        <TextBox x:Name="textBox" Height="40" Margin="155,10,247,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Right" Height="50" Margin="0,10,77,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="135"/>
        <DataGrid x:Name="dataGrid" Margin="10,85,10,10" xmlns:local="clr-namespace:WpfApplication1">
            <DataGrid.Resources>
                <local:Converter x:Key="conv" />
            </DataGrid.Resources>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource conv}">
                                <Binding Path="." />
                                <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

但是,我想制作另一个列表,例如d.images,我可以在for循环中添加图像,然后将其添加到dataGrid,就像其他一样列。

最后,我尝试有5列,其中4列为double,其中一列包含图像。

谢谢。

1 个答案:

答案 0 :(得分:1)

例如,您可以向Uri类添加MyDataObject属性,其中包含图像的路径:

public class MyDataObject
{
    public double AmountNeed { get; set; }
    public double TotalLose { get; set; }
    public double TotalGain { get; set; }
    public double TotalCost { get; set; }
    public Uri ImageSource { get; set; }
}

然后,您可以添加一个DataGridTemplateColumn,其中包含绑定此属性的Image元素:

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Margin="10,85,10,10" xmlns:local="clr-namespace:WpfApplication1">
    <DataGrid.Resources>
        <local:Converter x:Key="conv" />
    </DataGrid.Resources>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource conv}">
                        <Binding Path="." />
                        <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="AmountNeed" Binding="{Binding AmountNeed}" />
        <DataGridTextColumn Header="TotalLose" Binding="{Binding TotalLose}" />
        <DataGridTextColumn Header="TotalGain" Binding="{Binding TotalGain}" />
        <DataGridTextColumn Header="TotalCost" Binding="{Binding TotalCost}" />
        <DataGridTemplateColumn Header="Image">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding ImageSource}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
for (int z = 0; z<list_Exp.Count; z++)
{

    var d = new MyDataObject();

    d.AmountNeed = Math.Ceiling((goalexp - currentexp) / (list_Exp[z]));
    d.TotalLose = d.AmountNeed* (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]);
    d.TotalGain = d.AmountNeed* list_AmountMade[z] * list_SellPrice[z];
    d.TotalCost = d.TotalGain - d.TotalLose;
    d.ImageSource = new Uri(@"c:\test\pic.png", UriKind.RelativeOrAbsolute);

    L.Add(d);
}