Align WPF window on startup - wpf

I have a WPF window which is positioned incorrectly when shown.
I want it to be positioned correctly, like this:
How may I accomplish this?
This is the xaml for the window:
<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" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="1050" Width="910" Background="{x:Null}" >

There's a very handy XAML tag called WindowStartupLocation that allows you to specify where it starts. It is used like this:
<Window
...
Title="MainWindow"
Height="1050"
Width="910"
Background="{x:Null}"
WindowStartupLocation="CenterScreen">
This will start it in the center of the window where the mouse pointer is.
Edit: There is also an option called CenterOwner if you want it to always be the same monitor you start your app on.

Try the following (it will bind the top of your window to the top of the screen):
<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" mc:Ignorable="d" x:Class="WpfApplication9.MainWindow"
Title="MainWindow" Height="1050" Width="910" Background="{x:Null}" WindowStartupLocation="Manual" Top="0">

Related

Can I use a resource declared in its own XXX.Resources?

I've a Window, in which I declare one Resource, I was hopping to be able to use it in the window itself, but I'm not sure this is possible?
<dx:ThemedWindow x:Class="SomeWindow"
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:dxco="http://schemas.devexpress.com/winfx/2008/xaml/controls"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:frameworkExtensions="clr-namespace:SomeNamespace.FrameworkExtensions"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" ContentTemplate="{StaticResource dataTemplate}" >
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
[...]
</dx:ThemedWindow>
Here by example it's for a custom DataTemplate.
How should I do this? Because currently Visual Studio is warning me that it can't find "dataTemplate"
Your problem is because you're trying to use a resource before it's there.
Let's consider a simple example.
If you did:
<Window x:Class="WpfApp1.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Window.Background>
<StaticResource ResourceKey="TestResourceBrush"/>
</Window.Background>
<Grid>
</Grid>
</Window>
Then you see no error and the background of the window turns blue.
If you instead do:
<Window x:Class="WpfApp1.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{StaticResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>
Then that will error, saying it can't resolve the resource.
If you made that a DynamicResource, then that works ok.
<Window x:Class="WpfApp1.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="400"
Background="{DynamicResource TestResourceBrush}"
>
<Window.Resources>
<SolidColorBrush x:Key="TestResourceBrush" Color="Blue"/>
</Window.Resources>
<Grid>
</Grid>
</Window>
The way resources work is that the control looks for the resource in itsself, then in the parent then all the way up the visual tree until it hits the application. see here
so if you had the resource declared on a listview then the panel it is contained within can't use the resource but the list view can.
i suspect the issue you're having is that the template doesn't exist yet because you haven't compiled your code. because wpf is so flexible the designer actually has to run the code to see how it works so if your code hasn't been compiled or has changed since the last compilation the designer can't see the changes.
failing that it may just be order of execution, ie your consuming your template in an attribute but creating it in a child element, in this case move the contentTemplate to an element below the resources element and you should have access to it.
ie
<dx:ThemedWindow.Resources>
<frameworkExtensions:ConventionBasedDataTemplateSelector x:Key="dataTemplate"/>
</dx:ThemedWindow.Resources>
<dx:ThemedWindow.ContentTemplate>
<StaticResource ResourceKey="dataTemplate"/>
</dx:ThemedWindow.ContentTemplate>

TaskBar not shown when we set WindowChrome for Window

I set the system taskbar to autohide, then I ran the WPF application in Maximized state. When the application is in maximized state, I mouse hover the bottom of the screen to view the taskbar but it does not appear. Find the code block below.
<Window x:Class="_189619.MainWindow"
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:_189619"
mc:Ignorable="d" WindowState="Maximized"
Title="MainWindow" Height="350" Width="525">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="50" ResizeBorderThickness="3"/>
</WindowChrome.WindowChrome>
<Grid>
</Grid>
</Window>
Any workaround to show the TaskBar?
Thanks in Advance.
Regards,
Priyanga B

Winform control getting cropped in WPF

I'm having problem trying to include a winform user control in WPF. My control gets cropped whatever I try to do.
The control keeps autosizing itself even when I have not set the option.
Here's what it looks like
Here's the XAML
<Window x:Name="mainForm" x:Class="RFID.MainWindow"
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:RFID"
mc:Ignorable="d"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:sp="clr-namespace:gei1076_tools;assembly=gei1076_tools"
Title="MainWindow" Height="350" Width="755.682">
<Grid>
<WindowsFormsHost>
<sp:SerialPortConfigurator x:Name="spcConfigurator" ></sp:SerialPortConfigurator>
</WindowsFormsHost>
</Grid>
</Window>
What am I missing?
Thanks

Use user control in xaml

I think I'm missing something very obvious.
I create a WPF application and a user control. Both within one project in Visual Studio. In the WPF application I want to use the user control but the compiler pretends not to know the user control:
Error 1 UserControl1 is not supported in a Windows Presentation Foundation (WPF) project
Here is the WPF application XAML:
<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>
<UserControl1></UserControl1>
</Grid>
And this is the XAML code of my control
<UserControl x:Class="WpfApplication1.UserControl1"
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="300" d:DesignWidth="300">
<Label>Hello World</Label>
You have missed declaration of the namespace in your xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1></local:UserControl1>
</Grid>
</Window>

WPF Error: Do not recognize or can not access the member "resource"

Ok, I have to say that I just started using WPF and I know nothing about XAML, so for my first project with WPF, I'll be trying to build my own app for my own buisness, But im getting a problem trying to get an image resource.
This is what I tried
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background="{StaticResource Circuits}">
<Application.resource>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Application.resource>
</Window>`
But at the <Application.resource> line I'm getting the error
Do not recognize or can not access the member "resource"
and I just dont know what to do
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background="{StaticResource Circuits}">
<Window.Resources>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Window.Resources>
</Window>`
Have you tried :
<Application.Resources>
So in your exemple :
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="605.027" Width="1042.214" WindowStyle="None" Background=" {StaticResource Circuits}">
<Application.Resources>
<ImageBrush x:Key="Circuits" ImageSource="Circuits.jpg"/>
</Application.Resources>
</Window>

Resources