我制作了一个包含button
和dataGrid
的用户界面。
dataGrid
与SampleDataSource
绑定,后者是一列图片。
单击按钮后,dataGrid
应添加4个列表(dataGrid
列),因此最终输出应为dataGrid
,其中包含5个列表: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>
但是,我得到的问题是当我点击button
以使用以下代码将数据添加到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;
d.ImageSource = new Uri(@"path....", UriKind.RelativeOrAbsolute);
L.Add(d);
}
dataGrid.ItemsSource = L;
它会删除包含SampleDataSource
图片的列。
如何将4列添加到datagrid
而不是替换图像列?
谢谢。