WPF Set Grid background for resource image

This is perplexing, in winforms it is unforgivably easy, but in WPF it seems impossible.

I need to set the grid background to the image, it must be just what I thought.

The image was configured as a resource (right-click on the project name → properties → resource tab → import an existing file), but when I click on the "Background" property and select a brush for the tile, it points me to the file that is imported into the folder " Resources ", this works until the application is launched from outside the visual studio where it does not work.

<Grid Width="550" Height="350">
    <Grid.Background>
        <ImageBrush ImageSource="Resources/CINTRA2016.png"/>
    </Grid.Background>

I have the code above in XAML, how do I work with resources? I also tried <ImageBrush ImageSource="pack://application:,,,/CINTRA 2016;CINTRA2016"/>that did not work.

Both images have a resource build action in Solution Explorer

+4
source share
1 answer

Perhaps your path is wrong. Try using

<Grid Width="550"
      Height="350">
    <Grid.Background>
        <ImageBrush ImageSource="pack://application:,,,/WpfApplication1;component/Resources/CINTRA2016.png" />
    </Grid.Background>
</Grid>

where WpfApplication1is the name of your project and the Resourcesfolder containing the image.

+2
source

Source: https://habr.com/ru/post/1621285/


All Articles