如何用图片设置矩形的背景?

Bub*_*ble 1 c# wpf background

我创建一个矩形

public void Set(Rectangle maps, int y, int x) {
    Map.Children.Add(maps);
    maps.SetValue(Grid.RowProperty, x);
    maps.SetValue(Grid.ColumnProperty, y);

}
Run Code Online (Sandbox Code Playgroud)

但如何用"Resources/1.jpg"改变背景?

Jon*_*zzi 6

像这样:

<Rectangle>
    <Rectangle.Fill>
        <ImageBrush ImageSource="/YourAppName;component/Resources/1.jpg" />
    </Rectangle.Fill>
</Rectangle>
Run Code Online (Sandbox Code Playgroud)

再次编辑(对不起)

或者在C#中

maps.Fill = new ImageBrush {
    ImageSource = new BitmapImage(new Uri(@"pack://application:,,,/YourAppName;component/Resources/1.jpg", UriKind.Absolute))
};
Run Code Online (Sandbox Code Playgroud)