0

使用 C#.NET4.5、Visual Studio 2012、WPF。

好吧,在伟大的人和建议的帮助下,我走了多远!

为图像设置一个新列:

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 来检查值:

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
                }
    }
}

因此,正如您所看到的,我逐步将其放入一些嵌套条件中,然后评论所在的位置就是我要添加图像的位置,例如:

paretogrid.columns[12].setvalue(can image go here? asks for dependency);

不知道如何添加图像我所看到的只是通过项目源将图像添加到整个列。

我哪里错了?

编辑:2013年 8 月 4 日 好的,有两个建议,到目前为止没有发生错误,这对我来说总是很好。不幸的是没有图像出现。

Datagrid.Children.Add();

出于某种原因,我的数据网格在智能感知中没有这个 .Children 方法,即使我强制它只是红线我。我错过了什么?

XAML 上不大,所以这里是网格。

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">
4

1 回答 1

2

我没有完全符合您的环境(VS2012 和 Windows 8),但在 wpf 中,您也可以使用该SetValue()方法访问属性。你可以尝试使用这样的东西:

 Image img = new Image();
 img.SetValue(Grid.ColumnProperty, "2");
 img.SetValue(Grid.RowProperty, "1");

希望能帮助到你。

我在我的机器上测试了一个小演示,效果很好。这是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 必须设置为 Resource。

于 2013-04-05T15:47:50.340 回答