WPF将图像添加到列中的单元格 [英] WPF add image to cell in column
问题描述
使用C#.NET4.5,Visual Studio 2012,WPF。
Using C#.NET4.5, Visual Studio 2012, WPF.
在这里,我从伟大的人和建议中得到了很多帮助!
Well here's how far I got with a lot of help from great people and advice!
为图像设置一个新列:
DataGridTemplateColumn p1 = new DataGridTemplateColumn();
p1.Header = "p1";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
Binding b1 = new Binding("picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(System.Windows.Controls.Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
p1.CellTemplate = cellTemplate1;
paretogrid.Columns.Add(p1);
然后我检查每个行并设置一些Ifs来检查值:
Then I check each "row" and set up some Ifs to check values:
private void ShowArrows()
{
var rows = GetDataGridRow(paretogrid);
foreach (DataGridRow r in rows)
{
DataRowView rv = (DataRowView)r.Item;
var par3 = paretogrid.Columns[7].GetCellContent(paretogrid.Items[2]) as TextBlock;
int pconv3 = Convert.ToInt32(par3.Text);
var par2 = paretogrid.Columns[8].GetCellContent(paretogrid.Items[2]) as TextBlock;
int pconv2 = Convert.ToInt32(par2.Text);
var par1 = paretogrid.Columns[9].GetCellContent(paretogrid.Items[2]) as TextBlock;
int pconv1 = Convert.ToInt32(par1.Text);
var parNew = paretogrid.Columns[10].GetCellContent(paretogrid.Items[2]) as TextBlock;
int pconvNew = Convert.ToInt32(parNew.Text);
if(pconv3 == pconv2)
{
paretogrid.Columns[12].
}else
if(pconv3 > pconv2)
{
//uparrow
}
else
if (pconv3 < pconv2)
{
//down
}
}
}
因此,您可以看到我单步执行,将其抛入几个嵌套条件,然后评论所在的位置是我要添加图像的位置,如:
So as you can see I step through, throw it into a few nested conditions then where the comments are is where I want to add the images, something like :
paretogrid.columns[12].setvalue(can image go here? asks for dependency);
不确定如何添加图像我看到的是通过项目源将图像添加到整个列。
not sure how to add the image all I see is adding images to an entire column via the item source.
我哪里错了?
编辑:08/04/2013
好了两个建议,到目前为止没有发生任何错误,这对我来说总是很好。不幸的是,没有出现任何图像。
08/04/2013 Ok got two suggestions, so far no errors are happening which is always nice to me. unfortunatly no images are showing up.
Datagrid.Children.Add();
由于某些原因我的数据网格在智能感知中没有这个.Children方法,即使我强迫它它只是让我变红了。我错过了什么?
for some reason my datagrid does not have this .Children method in the intellisense, even when I force it it just redlines me. What am I missing?
在XAML上不大,所以继承网格。
Not big on XAML so heres the grid.
Grid Margin="10,13,6,-13" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" HorizontalAlignment="Left" Width="1442">
<DataGrid Name ="paretogrid" HorizontalAlignment="Left" Height="500" Margin="16,63,0,0" VerticalAlignment="Top" Width="1126" RenderTransformOrigin="0.5,0.5" Background="{x:Null}" FontSize="14" SelectionChanged="paretogrid_SelectionChanged">
推荐答案
我没有完全适合您的环境(VS2012和Windows 8),但在wpf中,您还可以使用 SetValue()
方法访问属性。您可以尝试使用以下内容:
I don't have exactly your environment (VS2012 and Windows 8), but in wpf you can access properties also with the SetValue()
method. You could try to use something like this:
Image img = new Image();
img.SetValue(Grid.ColumnProperty, "2");
img.SetValue(Grid.RowProperty, "1");
希望有所帮助。
我有在我的机器上测试了一个小型演示,它运行良好。这是xaml:
I have tested a small demo on my machine and it works well. Here is the xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="paretoGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
以及后面的代码:
var img = new Image {Width = 100, Height = 100};
var bitmapImage= new BitmapImage (new Uri(@"pack://application:,,,/Images/old-go-down.png"));
img.Source = bitmapImage;
img.SetValue(Grid.RowProperty, 1);
img.SetValue(Grid.ColumnProperty, 1);
paretoGrid.Children.Add(img);
图像构建操作必须设置为资源。
Image Build Action has to be set as Resource.
这篇关于WPF将图像添加到列中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!