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.
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);
}
}
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.
Related
How do you get a static background image in WPF. With an image set using the imagebrush it always expands or contracts when I resize or extend my window. How do I make the image static so when I extend a window more of the image is revealed and not resized? ie - similar to web design css or something like that.
set ImageBrush property Stretch to None (default is Fill).
<Window.Background>
<ImageBrush Stretch="None" Source="src"/>
</Window.Background>
Might also need to anchor it to a corner (probably top left) using AlignmentY & AlignmentX.
<Window.Background>
<ImageBrush Stretch="None"
Source="src"
Viewbox="0.25, 0.25, 1, 1"
AlignmentY="Top"
AlignmentX="Left"/>
</Window.Background>
I am relatively new to WPF and trying to insert a label computed by an algorithm inside a panel. I create the label in my code. I do not use the designer for the label.
The problem is that when I add the label to the panel, it gets resized when I resize the Window. I want the label to be a fixed height and width. Apparently it is anchored to the bottom of the window (I know that from the usual Winforms behavior).
How can I unanchor it, or give it a fixed size?
EDIT
XAML:
<Window x:Class="TestWrapper.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyTest" Height="483.833" Width="525" Loaded="Window_Loaded">
<Grid x:Name="MyGrid" Margin="0">
<Grid.Background>
<ImageBrush ImageSource="Resources/Background.jpg" Stretch="Uniform"/>
</Grid.Background>
</Grid>
</Window>
Code:
if (!_initialized)
{
Label lbl = new Label()
{
Content = "Test",
FontSize = 36,
Foreground = new SolidColorBrush(Colors.Red),
Background = new SolidColorBrush(Colors.Black),
HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Top
};
this.MyGrid.Children.Add(lbl);
lbl.Margin = new Thickness(0, this.MyGrid.ActualHeight + lbl.ActualHeight, 0, 0);
this._initialized = true;
}
If you simply want to hardcode a width and height, explicitly set the Width and Height attributes of your label:
<Label Width="100" Height="20">Hello World</Label>
A Grid will automatically centre and stretch its children, given no other information, and this is the default behaviour you're likely seeing.
(If you provide more code/markup, I can give a more detailed explanation.)
I suggest to use instead the HorizontalAlignment and VerticalAlignment.
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="Hello World" />
In this way, the label is automatically centered and the size is dynamically adapted by the WPF Engine.
Remember also that a WPF interface should not be developed by code behind, in most of cases, if not always. You can easily use the Xaml to resolve your problem.
I create a Rectangle
public void Set(Rectangle maps, int y, int x) {
Map.Children.Add(maps);
maps.SetValue(Grid.RowProperty, x);
maps.SetValue(Grid.ColumnProperty, y);
}
But How to change the background with "Resources/1.jpg"?
Like this:
<Rectangle>
<Rectangle.Fill>
<ImageBrush ImageSource="/YourAppName;component/Resources/1.jpg" />
</Rectangle.Fill>
</Rectangle>
EDITED AGAIN (Sorry)
Or in C#
maps.Fill = new ImageBrush {
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/YourAppName;component/Resources/1.jpg", UriKind.Absolute))
};
I was having trouble using the "/YourAppName;" portion of the address as suggested by #Jonny Piazzi. It probably works, I just couldn't get it to. Alternatively, I was able to make this method work.
1) I added the image to my project in a folder I created: Images > Backgrounds > JellyFishBackground.jpg
2) I right clicked the image in Solution Explorer > Properties > Set Build Action to Resource
3) Build project
4) Simply target the image as such: (in my case I targeted a row of my grid, and used a Stretch property, which are beyond the scope of this question, just fyi to avoid confusion)
<Rectangle Grid.Row ="0">
<Rectangle.Fill>
<ImageBrush ImageSource="/Images/Backgrounds/JellyFishBackground.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
I need to convert my WPF application, which has a Ribbon, to use vector graphics for the images on buttons, etc., instead of bitmaps. I've got this working so far, except for the RibbonApplicationMenu items, which use their RibbonApplicationMenu.ImageSource property, with type ImageSource to set the graphic that shows up at the left side of the ribbon menu item.
My vector graphics are defined like this in XAML:
<ControlTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ViewBox Width="148.854" Height="150.500">
<Canvas Width="148.854" Height="150.500">
<Path>
<!-- . . . yadda yadda yadda . . . -->
</Path>
</Canvas>
<Viewbox>
</ControlTemplate>
These render properly when I set them to be the .Template of a Button, etc.
I can't figure out, however, how to use them for the images that show up in the RibbonApplicationMenu controls. Just setting the .Template property of the RibbonApplicationMenu does not work (it fills the entire control seems to break its functionality).
I attempted to use a VisualBrush and then render it to a RenderTargetBitmap to use as the .ImageSource (I also have to work mostly in code behind for reasons I won't get into):
ContentControl cc = new ContentControl(); // just picked ContentControl as a test
cc.Template = myTemplate; // assume myTemplate has the proper ControlTemplate
VisualBrush visBrush = new VisualBrush();
visBrush.Visual = cc;
// not really sure about the parameters to this next line
RenderTargetBitmap rend =
new RenderTargetBitmap(148.854, 150.5, 120, 96, PixelFormats.Pbgra32);
rend.Render(cc);
RibbonApplicationMenuItem menu = new RibbonApplicationMenuItem();
menu.ImageSource = render;
This compiles, but just shows a blank where the image would go. If I instead use a BitmapImage loaded from an image file as the .ImageSource, it displays correctly.
Any ideas how I can get this to work?
Two things that might need to be done:
Firstly there is no explicit size set for the Canvas in the ControlTemplate, you'll need to assign it a width and height (see the upvoted answer here for details).
The other thing is that you need to Arrange and Measure the Viewbox so that it assumes the required dimensions.
So altering the xaml for the vector graphics to something like:
<ControlTemplate x:Key="Template">
<Viewbox>
<Canvas Height="100" Width="130">
<Path>
<!-- data -->
</Path>
</Canvas>
</Viewbox>
</ControlTemplate>
And the code to:
ControlTemplate controlTemplate =
FindResource("Template") as ControlTemplate;
ContentControl content = new ContentControl();
content.Template = controlTemplate;
// ensure control has dimensions
content.Measure(new Size(200, 200));
content.Arrange(new Rect(0, 0, 200, 200));
RenderTargetBitmap render =
new RenderTargetBitmap((int)content.ActualWidth, (int)content.ActualHeight, 120, 96, PixelFormats.Pbgra32);
render.Render(content);
RibbonApplicationMenuItem menu = new RibbonApplicationMenuItem();
menu.ImageSource = render;
Will hopefully get it working.
I'm creating an application for WP7 where the background of it (a grid) receives the image from the camera. Here is how it works:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1">
<Grid.Background>
<VideoBrush x:Name="video" />
</Grid.Background>
</Grid>
C#:
Microsoft.Devices.PhotoCamera m_camera;
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += (s, e) =>
{
m_camera = new Microsoft.Devices.PhotoCamera();
video.SetSource(m_camera);
};
}
My question: Is there a way to access the zoom options of the camera with this? Or can I define the source as the camera totally zoomed in or out?
Unfortunately, no. You can manipulate a higher resolution image to fill the background with only the part that you'd like to zoom in on. See resolution example here