Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将来自2个源的数据绑定到dataGrid_C#_List_Xaml_Data Binding_Datagrid - Fatal编程技术网

C# 将来自2个源的数据绑定到dataGrid

C# 将来自2个源的数据绑定到dataGrid,c#,list,xaml,data-binding,datagrid,C#,List,Xaml,Data Binding,Datagrid,我制作了一个包含按钮和数据网格的UI。 dataGrid与SampleDataSource绑定,后者是一列图像 单击按钮后,dataGrid应再添加4个列表(列到dataGrid),因此最终输出应为带有5个列表的dataGrid:1个图像4个双列表 XAML如下所示: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present

我制作了一个包含
按钮
数据网格
的UI。
dataGrid
SampleDataSource
绑定,后者是一列图像

单击按钮后,
dataGrid
应再添加4个列表(列到
dataGrid
),因此最终输出应为带有5个列表的
dataGrid
:1个图像4个双列表

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">
    <Window.Resources>
        <DataTemplate x:Key="Property1Template">
            <StackPanel>
                <Image Source="{Binding Property1}" HorizontalAlignment="Left" Height="64" Width="64"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="60" Margin="10,10,0,0" VerticalAlignment="Top" Width="170" Click="button_Click"/>
        <TextBox x:Name="textBox" Height="60" Margin="195,10,197,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Right" Height="60" Margin="0,10,47,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="130"/>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Margin="10,85,10,10" xmlns:local="clr-namespace:WpfApplication1" ItemsSource="{Binding Collection}">
            <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>
                <DataGridTemplateColumn CellTemplate="{StaticResource Property1Template}" Header="Property1"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>
它从
SampleDataSource
中删除包含图像的列

如何将要添加到
datagrid
中的4列替换为图像列

多谢各位

        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;
            d.ImageSource = new Uri(@"path....", UriKind.RelativeOrAbsolute);
            L.Add(d);

        }

        dataGrid.ItemsSource = L;