WindowChrome is not supported in a WPF project - c#

I'm brand new to WPF after using WinForms for years. I'm trying to get a window that looks like a typical windows form with the blue border and red close button, or really anything that has a border like it shows in the middle 2 windows of the Microsoft Docs. This is for .Net framework up to 4.8.1 but I am using .net 6.
but my app always shows with no border:
Here is my MainWindow.xaml:
<Window x:Class="Sim.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:Sim"
mc:Ignorable="d"
Title="Simulator" Height="650" Width="1000"
Icon="Resources/Sim.ico"
WindowStyle="SingleBorderWindow">
<Grid>
<Button x:Name="Power" Content="Power" HorizontalAlignment="Left" Margin="84,84,0,0" VerticalAlignment="Top" Click="OnPowerClick"/>
</Grid>
</Window>
I tried to add WindowChrome after WindowStyle="SingleBorderWindow">
<Setter Property="shell:WindowChrome.WindowChrome">
</Setter>
but I get the error "WindowChrome is not supported in a Windows Presentation Foundation (WPF) project."
Is there a way to get an older style border in .net 6 wpf?

Related

Problem With Hotbar, arrow as web site in .net framework wpf app

Image with Problem
This is a WPF application launched with debug of visual studio
I've a window, where show content
<Window x:Class="Test.All"
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:Test"
mc:Ignorable="d"
Title="All" Height="450" Width="800">
<Grid>
</Grid>
and a user control, with the content to show in the window
<UserControl x:Class="Test.ShowContent"
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:Test"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button x:Name="btnrand" Content="button" HorizontalAlignment="Left" Margin="300,69,0,0" VerticalAlignment="Top" Width="200" RenderTransformOrigin="0.473,0.477" Height="58" Click="btnrand_Click"/>
<Label x:Name="showblasph" Content="" HorizontalAlignment="Left" Margin="60,169,0,0" VerticalAlignment="Top" Height="163" Width="664" FontSize="24"/>
</Grid>
</UserControl>
With a WinForms application, I did not have that problem, but with wpf yes, any ideas?
The problem is the arrows at the top
You are accidentially using a NavigationWindow instead of a Window. A navigation window has built-in browser-like controls. These controls are not shown in the Visual Studio designer, but at runtime. The black bar at the top are the XAML debugging tools, that you can enable or disable in debug mode.
A navigation window is used automatically, if you specify a UserControl resource as StartupUri. What type of window is used or created depending on the startup URI is documented here.
Types that display a NavigationWindow when set as StartupUri resource:
NavigationWindow
Page
UserControl
FlowDocument
PageFunction<T>
Therefore, you have to set a Window as StartupUri and set its Content to your UserControl.

WPF Window with fixed size doesnt display all contents

I just started working with WPF and want to create a simple app.
However, when I place buttons at the bottom of the window and launch the app - they get cut off
Has anyone encountered this problem?
Designer preview shows this
But then when launching the app button gets cut off
XAML code is as follows:
<Window x:Class="DXF2SVG.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:DXF2SVG"
mc:Ignorable="d"
ResizeMode="NoResize"
Title="MainWindow" Height="400" Width="400">
<Canvas Width="400" Height="400">
<Button x:Name="btn_LoadDXFFile" Content="Load .dxf File" Width="100" Height="40" Canvas.Top="334" Canvas.Left="150" />
</Canvas>
</Window>
Thanks in advance!
Everyone who said canvas is evil is right
#NawedNabiZada 's answer about grid and vertical and horizontal alignment did the trick
thanks everyone!

Strange TextBox behavior in Windows 8.1 and windows 10

The following code works like expected in Windows 7:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication2.MainWindow"
Title="MainWindow"
Height="Auto"
Width="500"
ResizeMode="NoResize"
WindowStyle="SingleBorderWindow"
SizeToContent="Height"
WindowStartupLocation="CenterScreen">
<Grid>
<TextBox IsReadOnly="True"
MinLines="6"
MaxLines="6"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Visible"
Text="test
test
test
test
test
test
test
test
test
"/>
</Grid>
The same code running under Win8.1 or Win10 shows the TextBox with its vertical scrollbar centered vertically within a larger TextBox area.
It seems to be a WPF layout problem. The code was developed in VS2013, .net 4.5. All tested windows systems are x64.
Any ideas to make this simple app running with same results in win7 ... win10?
Remove the line
MaxLines="6"
it is causing your Textbox to be limited in height and appear vertically centered in Window

Windows Forms Chart Collection Editors in WPF

I am a new WPF user, coming from Windows Forms and I have a question regarding Windows Forms Control Integration in WPF.
Windows Forms offer a very extensive built-in Data Visualization Chart control, which I would like to use in a WPF application.
I successfully added the chart control to the application, using a WindowsFormsHost control. This is how the XAML code looks:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winformchart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Name="mainWindow" x:Class="WPFChartControl.MainWindow"
Title="WPF Chart Control" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Grid d:IsLocked="True">
<WindowsFormsHost x:Name="chartHost" HorizontalAlignment="Left" Height="260" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" d:IsLocked="True">
<winformchart:Chart x:Name="MyChart" Dock="Fill">
<winformchart:Chart.Series>
<winformchart:Series Name="series" ChartType="Line"/>
</winformchart:Chart.Series>
<winformchart:Chart.ChartAreas>
<winformchart:ChartArea/>
</winformchart:Chart.ChartAreas>
</winformchart:Chart>
</WindowsFormsHost>
<Button Content="Add Point" Click ="Button_Click" Margin="221,275,221,0" VerticalAlignment="Top" d:IsLocked="True"/>
</Grid>
</Window>
And now my question: Is there any way to access the Chart Collection Editors (Annotations, ChartArea, Legends, Series, Titles) from Visual Studio's Property window at design time, similar to how they can be accessed when developing Windows Forms applications? I find these editors very convenient to use, compared to customizing all the chart properties from code.
Windows Forms Properties
ChartArea Collection Editor

WPF window gets minimized instantly

SOLUTION here
I was asked to emulate a windows forms context menu using a WPF window. I created a window when the user clicked a certain region, but that window got minimized to the task bar instantly. I need it to stay on top of other windows at least until it loses focus or is deactivated. Its window style is none, and it doesn't matter whether its show in taskbar property is true or false. Similar questions exist on SO, but they don't seem to solve anything in my case. Why this odd behavior? Is there a fix for it?
<Window x:Class="InterceptRouteWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Width="200" ResizeMode="NoResize" SizeToContent="Height" ShowInTaskbar="False" Loaded="Window_Loaded" WindowStyle="None" Height="auto" Deactivated="Window_Deactivated">
<StackPanel Height="auto" Name="stackPanel1" Width="200" Background="WhiteSmoke">
<Separator Height="5" FlowDirection="LeftToRight">
<Separator.Background>
<SolidColorBrush />
</Separator.Background>
</Separator>
</StackPanel>
</Window>
So it's a simple window. The way I invoke it:
IRWindow = gcnew InterceptRouteWindow(routes, fpId, offsetPoint.x, offsetPoint.y);
GetMainFrame()->IRWindow->Show();
This is triggered by a click event inside an older app, a mixture of MFC and WindowsForms.. not my code, I had to obey their design and use WPF where I could.

Categories