Strange TextBox behavior in Windows 8.1 and windows 10 - c#

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

Related

WindowChrome is not supported in a WPF project

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?

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!

XAML Window AllowsTransparency=True and ComboBox.IsEditable=True causes display error in WinXP

I have a WPF window, in which I have set the WindowStyle=None, AllowsTransparency=true. I have an editable combobox and a textbox which background is red in the window. When I run the application in Windows XP, textbox and combobox don't display correctly:
TextBox's background changes to transparent
TextBox's text disappear
ComboBox's border disappear
But I set Combobox.Editable=False or run this application in Win7, there is no problem. Why?
Please help me, thank you!
My environment:
WinXP SP3 + .Net Framework 4.0
Code:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="300"
AllowsTransparency="True"
WindowStyle="None"
Background="White">
<StackPanel>
<TextBox Text="aaa" Height="30" Background="Red"></TextBox>
<ComboBox IsEditable="True" Height="30">
<ComboBoxItem>aaa</ComboBoxItem>
<ComboBoxItem>bbb</ComboBoxItem>
<ComboBoxItem>ccc</ComboBoxItem>
</ComboBox>
</StackPanel>
</Window>
As per discussion in comments and looking at the posted image, seems like everything went transparent
you may perhaps as a workaround set a Background to StackPanel, this may apply a background to the controls within so they do not appear transparent.
eg
<StackPanel Background="White">
<TextBox Text="aaa"
Height="30"
Background="Red"></TextBox>
<ComboBox IsEditable="True"
Height="30">
<ComboBoxItem>aaa</ComboBoxItem>
<ComboBoxItem>bbb</ComboBoxItem>
<ComboBoxItem>ccc</ComboBoxItem>
</ComboBox>
</StackPanel>
I didn't try this workaround as I do not have a WinXP machine. you may give it a try and see if that helps.

UI Elements placed in the wrong place while running in full screen

I am having a C# WPF application that runs in full screen. When I place a button in the top right of the canvas and run the application the button doesn't appear. What i figured it out is that the canvas is larger than the screen although I made it the same width and height as the screen resolutions.
PS: The application is working fine on another computer and the canvas size is correct but when I run it on my laptop or PC the same problem appears.
<Window x:Name="Mywindow" x:Class="Graduation_Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" Height="768" Width="1366" WindowStyle="None"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
WindowState="Maximized" Topmost="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Canvas Name="workspace">
<Label x:Name="Print" Content="Label" Canvas.Left="57" Canvas.Top="719" Width="74"/>
</Canvas>
</Window>
Remove the Height="768" and Width="1366" properties from the Window.

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