I am working on a WPF application that has a WinformsHost element. I try to set the cursor
for the winforms host element, but this does not have any effect. I tried to use the ForceCursor property of the window, but this seems to be ignored completely.
In my sample window I have a WPF Textblock on the left with a Hand cursor which is used properly, but on the right side where I have a WinForms PropertyGrid, I get the same behavior.
<Window x:Class="WpfWinformsCursor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525"
Cursor="ScrollAll"
ForceCursor="False"
>
<Grid Cursor="Pen">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Cursor="Help">
<TextBlock Cursor="Hand" />
</Grid>
<WindowsFormsHost Grid.Column="1" Cursor="ScrollNW">
<wf:PropertyGrid Cursor="Hand" />
</WindowsFormsHost>
</Grid>
</Window>
Can somebody help me how to do this correctly?
Related
I have a window that expands to left and to the right.
Simplified example:
<Window x:Class="Application1.Windows.Test"
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"
Title="MainWindow"
SizeToContent="Width" Height="250">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" /> <!--Expander LEFT-->
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="175" /> <!--Red Content -->
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="Auto"/> <!--Expander RIGHT-->
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="*"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Expander Grid.Column="0"
Grid.Row="0"
Grid.RowSpan="3"
Margin="5"
ExpandDirection="Left">
<Grid Width="200">
</Grid>
</Expander>
<Grid Grid.Column="2"
Grid.RowSpan="3"
Background="Red"/>
<Expander Grid.Column="4"
Grid.Row="0"
Grid.RowSpan="3"
Margin="5"
ExpandDirection="Right">
<Grid Width="200">
</Grid>
</Expander>
</Grid>
Here you can see what the problem is:
I've already had a look on WPF - Expand Window to the Left.
The solution kind of works but that is only solving half of the problem.
How to do that for Left AND Right ?
UPDATE
I do not want the Expander columns to be fixed. They should just show the minimal space needed for the expander
One approach that will work is this:
(But I'm somehow unsatisfied with this solution)
Create an Enum to store the last Action in:
public enum ExpandActions
{
ExpandRight,
ExpandLeft,
CollapsRight,
CollapseLeft
}
Then I added the handlers for Expand & Collapse to my Expanders in xaml.
At last override SizeChanged on the Window as in WPF - Expand Window to the Left.
We only want this behavior, when expanding to left - otherwise we would kill our 'expand-to-right' behavior.
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
if (!sizeInfo.WidthChanged)
{
base.OnRenderSizeChanged(sizeInfo);
return;
}
Hide();
base.OnRenderSizeChanged(sizeInfo);
//Last action was on left expander
if(_lastAction == ExpandActions.CollapseLeft || _lastAction == ExpandActions.ExpandLeft)
{
Left -= (sizeInfo.NewSize.Width - sizeInfo.PreviousSize.Width);
}
Show();
}
Good point about this is that expansion out of screen could be handled.
It works but I guess it's not the best solution.
The result is this:
I am working on a C# wpf project and I am having an issue. I have a Main Window which hosts a grid. This grid hosts a User Control and the User Control hosts 3 DockPanels in a 3 column layout.
The 3 columns are adjustable via a grid splitter which is working however, the right hand side column isn't filling the available space of the Main Window grid.
Below is the XAML of my Main Window hosting the user control:
<Window x:Class="Boardies_Email_Client.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:Boardies_Email_Client"
mc:Ignorable="d"
Title="MainWindow" Height="350" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip" WindowState="Maximized">
<Grid>
<Grid x:Name="GridHost" Margin="0" Background="#FFF3FF00"/>
<StatusBar Height="24" VerticalAlignment="Bottom"/>
</Grid>
</Window>
Below is the XAML for the User Control
<UserControl x:Class="Boardies_Email_Client.EmailClient"
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:Boardies_Email_Client"
mc:Ignorable="d"
d:DesignHeight="505">
<Grid HorizontalAlignment="Left" Width="1120">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92*"/>
<ColumnDefinition Width="121*"/>
<ColumnDefinition Width="87*"/>
<ColumnDefinition Width="225*"/>
<ColumnDefinition Width="134*"/>
<ColumnDefinition Width="116*"/>
<ColumnDefinition Width="345*"/>
</Grid.ColumnDefinitions>
<DockPanel Background="#FFEC1717" Grid.ColumnSpan="2">
<Label x:Name="label" Content="My Label" Margin="0,0,0,460" Width="175"/>
</DockPanel>
<GridSplitter x:Name="gridSplitter" HorizontalAlignment="Left" Width="5" Grid.Column="2"/>
<DockPanel LastChildFill="False" Margin="5,0,0,0" Background="#FF5DFF00" Grid.Column="2" Grid.ColumnSpan="3"/>
<GridSplitter x:Name="gridSplitter1" HorizontalAlignment="Left" Width="5" Grid.Column="5"/>
<DockPanel Grid.Column="5" Margin="5,0,0,0" Background="#FF0A00E2" Grid.ColumnSpan="2"/>
</Grid>
</UserControl>
Below is a screenshot showing the problem
The red, green and blue are the dock panels separated by grid splitters. The yellow is the background colour of the the grid of the main window hosting the user control, the blue panel should be filling up the available space of the yellow but I can't see why it isn't.
I haven't hard set any widths that I can find.
#Boardy
I ran your code.
The splitting of the UserControl and organization of the Grid are fine.
But you set the width to a fixed size :
<UserControl ...>
<Grid HorizontalAlignment="Left" Width="1120">
If you remove the fixed size, everything is fine, the control takes the size of the window and you don't see other background (yellow or right) on the right for sizes larger than 1120 :
<UserControl ...>
<Grid >
Regards
I have a Window with the property AllowsTransparency set to true, and with the Background property set to a semi-transparent color, the code :
<Window x:Class="InstantSnip.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Main, Source={StaticResource Locator}}"
AllowsTransparency="True"
WindowStyle="None"
ResizeMode="NoResize"
Topmost="True"
Width="180" Height="80"
Background="#7FFFFFFF" >
<Border x:Name="LayoutRoot"
BorderBrush="#99FFFFFF"
BorderThickness="1"
CornerRadius="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource NewSnipButton}" Margin="5,0"/>
<Button Grid.Column="1" Style="{StaticResource ReTrySnippingButton}" Margin="5,0"/>
<Button Grid.Column="2" Style="{StaticResource CloseButton}" Margin="5,0"/>
</Grid>
</Border>
</Window>
This is how it looks like :
Now, I want to give the Background="#7FFFFFFF" to the LayoutRoot Border instead of the Window so that the color can fit exactly and only inside the Border's boder, which is not the case when the Background="#7FFFFFFF" is a property of the Window as you can see here :
And hence, and with no big changes, the code will simply be as follows :
<Window x:Class="InstantSnip.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Main, Source={StaticResource Locator}}"
AllowsTransparency="True"
WindowStyle="None"
ResizeMode="NoResize"
Topmost="True"
Width="180"
Height="80">
<Border x:Name="LayoutRoot"
BorderBrush="#99FFFFFF"
Background="#7FFFFFFF"
BorderThickness="1"
CornerRadius="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource NewSnipButton}" Margin="5,0"/>
<Button Grid.Column="1" Style="{StaticResource ReTrySnippingButton}" Margin="5,0"/>
<Button Grid.Column="2" Style="{StaticResource CloseButton}" Margin="5,0"/>
</Grid>
</Border>
</Window>
But what I get is this :
My first question is, why ?
My second question is, can you suggest a solution for this problem.
Set Background to Transparent in your Window that will do the trick
eg
<Window x:Class="InstantSnip.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Main, Source={StaticResource Locator}}"
AllowsTransparency="True"
WindowStyle="None"
ResizeMode="NoResize"
Topmost="True"
Width="180"
Height="80"
Background="Transparent" >
result
I don't have your styles so the button appears normal button
Edit
Explanation for your first question
As the default Background of a Window is usually White inherited from SystemColors.WindowColor. So unless you set any color which is fully or partially transparent, AllowsTransparency will not be able to compose transparency on it's own.
as any child control is rendered on window first then the whole window is composed to the background. So in short no matter what transparent color you set on your child controls it will be composed on a white background leading to no transparency.
so set Transparent on Window and then you can choose to have rounded corner border etc with desired transparent color.
From: Transparent Windows in WPF
WPF can obviously render transparent elements within its own window, but it also supports rendering the entire window with per-pixel transparency. This feature comes with a few issues.
If I'm reading your code correctly, you moved the Background property from the window to the border control. My guess, and it's only a guess, is that UserControls don't support transparent backgrounds.
This sounds similar to AllowTransparency alternative for rounded edge forms. Perhaps its accepted answer will work for you.
I'm trying to get my head around how WPF makes decisions when it renders a textblock with wrap enabled.
I have the following code:
<Window x:Class="WpfWrapTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="200" Height="200">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="5"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="Yellow" Grid.Column="0"></Border>
<GridSplitter ResizeDirection="Columns" Grid.Column="1" Width="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
<Grid Grid.Column="2" MinWidth="40">
<TextBlock TextWrapping="Wrap">asdflk;jsadlkfjlaskdjflasdkjflk laskdjfl;askjd l;kasjdf l;kjsadf ;lkajsdfl k</TextBlock>
</Grid>
</Grid>
When starting WPF decides to make my textblock larger than the screen and not take this into account for wrapping
Then when I drag the gridsplitter it somehow makes a different decision (probably because the gridsplitter is setting a width of a neighbour control?)
A third weird behaviour in this sample is when you try to drag the gridsplitter more left than it can go (minwidth on column 1 is 5). Then it decides to re-enlarge the textblock outside the visual screenspace.
What is making WPF do one or the other?
Try setting the 3rd column with to "*" instead. "Auto" means that the TextBlock will use as much space as it needs, effectively meaning it doesn't need to wrap.
When you drag the splitter, you are giving the grid column an explicit size, so TextBlock will wrap to fit to that size.
<Window x:Class="WpfApplication2.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>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="5"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="Yellow" Grid.Column="0"></Border>
<GridSplitter ResizeDirection="Columns" Grid.Column="1" Width="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></GridSplitter>
<Grid Grid.Column="2" MinWidth="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap">asdflk;jsadlkfjlaskdjflasdkjflk laskdjfl;askjd l;kasjdf l;kjsadf ;lkajsdfl k</TextBlock>
</Grid>
</Grid>
</Window>
It's quite simple really... probably too simple for an answer even.
If the TextBlock or a parent of the TextBlock is given a Width that the TextBlock.Text value is longer than, the text will be wrapped. Otherwise, it will not be wrapped.
It really is that simple. As for the other 'weird' symptoms that you spoke about, I can't really help you there... your code didn't show me them. For example, I'm not sure how you can drag a GridSplitter more left than it can go.
I'm new to WPF and am trying my hand at data binding, and haven't been able to find a solution to the problem that I am having. I am trying to make a window with a rectangle in it that I can dynamically switch between the left and right side of the screen, using the grid.column property. Here is my code so far, and I do not know where to go, if someone could suggest a decent website to read and solve my issue that would be nice too.
Here is my C# Code:
namespace WPFTestingApplication
{
public static class GridProperties
{
public static int gridColumn = 1;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
and my XAML code:
<Window x:Class="WPFTestingApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Name="Rect" Grid.Column="0" Fill="DarkGray" Margin="5" />
</Grid>
I want grid.column to be set to the gridColumn property in the GridProperties class.
<Window x:Class="WPFTestingApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTestingApplication"
Title="MainWindow" Height="200" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="{Binding Source={x:Static local:GridProperties.gridColumn}"
Name="Rect" Fill="DarkGray" Margin="5" />
</Grid>