在WPF中以编程方式将图像添加到网格时的默认位置

时间:2016-05-10 10:44:56

标签: c# wpf xaml

我正在尝试通过单击按钮在网格中添加图像。我做到了,但图像总是在窗口的中间。

我该怎么做才能进入左上角?

这是我的xaml代码:

var srv1 = new ComboItemExample { DisplayString = "Srv1", ConnectionString = "Data Source=tcp:10.0.0.110;Initial Catalog=Database1;User ID=user;Password=pass;" };
        cmbSrv.Items.Add(srv1);

xaml.cs的代码:

<Window x:Class="WpfApplication3.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">

    <ScrollViewer HorizontalScrollBarVisibility="Visible" Margin="0,0,0,0">
        <Grid Name="Grid" Margin="0,0,0,0">
            <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

        </Grid>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:0)

做你在Button上做过的事情。将HorizontalAlignment设置为Left,将VerticalAlignment设置为Top,如下所示:

var Mole = new Image();
Mole.HorizontalAlignment = HorizontalAlignment.Left;
Mole.VerticalAlignment = VerticalAlignment.Top;
...