Reputation: 389
I have an XAML code with binding to property MainImage:
<Grid>
<Grid.Background>
<ImageBrush ImageSource="{Binding MainImage}"/>
</Grid.Background>
</Grid>
MainImage is a ImageSource property. But, now I want to set simple Brush background of grid to. Application is using ImageSource, if there is an image, otherwise app have to set simple Brush color. There is the problem with covertation from Brush to ImageSource, if this is possible.
Upvotes: 1
Views: 253
Reputation: 1864
If your image is not transparent, I would simply create different Grid or even Rectangle, make it full screen and with color. If there is image available, then it will just cover the color and done.
Upvotes: 1
Reputation: 8161
I would use the OnLoaded event and check for nulls and change it that way. Unless you want to create an image and then paint it a certain color and return that as an image source. I don't think there are any fallbacks unless you roll your own.
<Grid x:Name="my_grid" Loaded="my_grid_Loaded">
<Grid.Background>
<ImageBrush ImageSource="{Binding MainImage}"/>
</Grid.Background>
</Grid>
private void my_grid_Loaded(object sender, RoutedEventArgs e)
{
Grid g = sender as Grid;
System.Windows.Media.ImageBrush ib = g.Background as ImageBrush;
if (ib.ImageSource == null)
{
g.Background = new SolidColorBrush(Colors.MYCOLOR);
}
}
Upvotes: 1