How to add multiple content control on same window [duplicate] - c#

Problem:
XAML file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="Test" Height="398.775" Width="782" BorderBrush="Black" Margin="0" IsTabStop="True">
<Window.TaskbarItemInfo>
<TaskbarItemInfo/>
</Window.TaskbarItemInfo>
<TextBox HorizontalAlignment="Left" Height="23" Margin="268,290,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Window>

A Window is a ContentControl. These have a Content property to display a single item. If you want to display multiple items, use one of the following built-in layout panels and drag them onto your window. Then you can add multiple controls to them.
Grid
StackPanel
WrapPanel
DockPanel
UniformGrid
Canvas

Because you're on the window who only accepts 1 element, you need first add a content controller .
It can be:
Grid
Canvas
Dock Panel
StackPanel
WrapPannel
Scroll viewer

Add <grid> </grid> in Window tag then you can add your controls in it.

Related

Why can't I put more than one control on a window?

Problem:
XAML file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="Test" Height="398.775" Width="782" BorderBrush="Black" Margin="0" IsTabStop="True">
<Window.TaskbarItemInfo>
<TaskbarItemInfo/>
</Window.TaskbarItemInfo>
<TextBox HorizontalAlignment="Left" Height="23" Margin="268,290,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Window>
A Window is a ContentControl. These have a Content property to display a single item. If you want to display multiple items, use one of the following built-in layout panels and drag them onto your window. Then you can add multiple controls to them.
Grid
StackPanel
WrapPanel
DockPanel
UniformGrid
Canvas
Because you're on the window who only accepts 1 element, you need first add a content controller .
It can be:
Grid
Canvas
Dock Panel
StackPanel
WrapPannel
Scroll viewer
Add <grid> </grid> in Window tag then you can add your controls in it.

WPF make container background not wash out the contents of children

Simple code:
<UserControl x:Class="MonitravLite.UserControls.Foo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MonitravLite.UserControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<!-- 1st example with setting background -->
<Grid Background="Blue" Opacity="0.5">
<TextBlock Text="Grid background"/>
</Grid>
<!-- 2nd example with grid on top-->
<Grid>
<TextBlock Text="Grid on top"/>
<Grid Background="Blue" Opacity="0.5"/>
</Grid>
</StackPanel>
I have various child controls that display states. When any of these children are in a error state, I want the background of the of the container to change color. e.g..e. if one of the children is in an error state, the background should be red.
However if I set the container background (grid in this example), it washes out the children.
If I do the second approach and lay a grid over the top - this doesn't happen?
I have tried setting the Panel.Zindex, but this doesn't help.
What am I missing to make the first example render like the second?
if you set the opacity of the grid all its children will inherit this property and become transparent as well! So instead of making the grid transparent choose a transparent color as a background.
instead of this line:
<Grid Background="Blue" Opacity="0.5">
try this:
<Grid Background="#7f0000FF">

Rubber window WPF

I create window with set Height and Width. Current content are placed in window. But when I change content rather big then previously it dont placed in window. How create rubber window who automatically change size under the desired?
<Window x:Class="GidroTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gidro_test="clr-namespace:GidroTest"
Title="MainWindow" Height="200" Width="200"
WindowStartupLocation="CenterScreen"
>
<DockPanel>
<!-- Before click -->
<Rectangle Width="199" Height="199" Click="Change_size_Click"/>
<!-- After click -->
<Rectangle Width="300" Height="300" Click="Change_size_Click"/>
</DockPanel>
</Window>
You can try set SizeToContent with: WidthAndHeight, Width, Height, don't forget remove Height, Width from Window
<Window SizeToContent="WidthAndHeight">
Details: https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent%28v=vs.110%29.aspx

Visual C# 2013 - Element disappearing when adding a new one in WPF form editor

When I try adding more than one element to my WPF form in the editor in VC#2013, the previous element disappears. In the end, I can't have more than one item in the form. I've already written some code so I'd prefer not starting again from scratch. The form has nothing special besides being borderless, fullscreen and starting maximized.
This is the XAML code for the form right now:
<Window x:Class="queue_bigscreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
<Label x:Name="nowServingLabel" Content="0" Margin="42,56,1160,131" Foreground="White" Height="893" FontSize="700" HorizontalContentAlignment="Center">
<Label.Effect>
<DropShadowEffect ShadowDepth="13"/>
</Label.Effect>
</Label>
</Window>
And this is what I get after I select a textbox and try adding it to the form:
<Window x:Class="queue_bigscreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
<TextBlock HorizontalAlignment="Left" Margin="1332,382,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
As you can see, the label disappears, and the textbox I added in turn disappears if I try adding something else. Am I doing something wrong or is it a known bug?
You need to put the items in a container. Window can only have a single root element, so to get multiple elements on the form, you need to have an element that allows children.
The closest to Windows Forms Form would be Grid. You can then put controls in that, with absolute and relative positioning. It's also the default, so I assume you accidentally deleted it from your XAML (or by being too aggressive with pressing delete in the designer).
Example form with a label and a textbox:
<Window x:Class="WpfApplication1.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">
<Grid>
<Label Content="Label" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="66,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>

How to center the image control inside Grid?

There is one Grid and I drop an Image control into the Grid.
What I do : just simply change both the property-HorizontalAlignment and VerticalAlignment to 'Center'.
However the image control performs strangely unlike other controls do. This Image control center itself according to its upper left corner like below :
I want to know why it performs in this way?
EDIT
Here is my XAML:
<UserControl x:Class="Entity.WPF.Controls.ShopProfile"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="780">
<Grid>
<DockPanel >
<Grid>
<Image HorizontalAlignment="Center" Height="100" Margin="0" VerticalAlignment="Center" Width="100"/>
</Grid>
</DockPanel>
</Grid>
And if I set margin like Margin="-50,-50,0,0",it is centered actually,but why other controls don't need this setting?
That's interesting, I'm not sure why that happens, or if it's documented somewhere.
To answer your question, how to center the image control inside a grid, just remove those properties and the image will be centered in the grid automatically.
<Grid>
<Image Height="100" Margin="0" Width="100" />
</Grid>

Categories