且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF将图像添加到列中的单元格

更新时间:2022-06-05 22:43:35

我没有完全适合您的环境(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.