Why doesn't WPF Canvas alow drop? - wpf

I have the following XAML for the main window:
<Window x:Class="ImageViewer.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="398" Width="434">
<Grid>
<Canvas AllowDrop="True" />
</Grid>
</Window>
But when I try to drag a file to the window, drop is not allowed. When Canvas is changed to ListBox, everything works perfectly.
How can the code be changed to allow drop to canvas?

By default, Canvas has no background so hit-testing is not picking up that the cursor is over the Canvas element, but is instead bubbling up to the Grid or Window which don't allow drop. Set the background to Transparent as follows and it should work:
<Window x:Class="ImageViewer.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="398" Width="434">
<Grid>
<Canvas AllowDrop="True" Background="Transparent" />
</Grid>
</Window>

This works like a charm! In code you would want to do something such as:
Canvas myCanvas = new Canvas();
myCanvas.AllowDrop = true;
myCanvas.Background = System.Windows.Media.Brushes.Transparent;

Related

Making PART of a WPF window click-through, but not its controls

In order to achieve a dropshadow effect, my window is slighty bigger than its main content with a transparent "border" around it. I am curious if there's a possibility to make this invisible border (coloured part in image) click-through only and prevent main content from being click-through.
This thread explains what to do in order to make the whole window click-through:
Making a WPF window click-through, but not its controls
Is there any way to adapt this approach for what I intend do achieve?
Only a window with a fully transparent background is really click through. For a semi-transparent window you could for example minimize the window yourself when clicking on the shadow, e.g.:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource == outer)
WindowState = WindowState.Minimized;
}
XAML:
<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"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300"
AllowsTransparency="True" WindowStyle="None">
<Window.Background>
<SolidColorBrush Color="Red" Opacity="0.3" />
</Window.Background>
<Grid x:Name="outer" Background="Transparent" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<Grid Background="Silver" Margin="10">
<TextBlock>GUI</TextBlock>
</Grid>
</Grid>
</Window>

initial size of element wpf

I have window with this markup
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Button Command="ApplicationCommands.Close">Foo</Button>
</Window>
I'd like to button have MinWidth and MinHeight 100
and MaxHeight and MinHeight equals to 500
And like size window to content so I make this
<Window x:Class="TestCloseWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" >
<Button MaxHeight="500" MaxWidth="500" MinHeight="100" MinWidth="100" Command="ApplicationCommands.Close">Foo</Button>
</Window>
And I'd like to set initial size of button 200 x 200
without writing Width and Height in window
If I write Width="200" Height="200" in button, button becomes unresizable.
How can I set initial size of control?
I am not sure exactly what you are trying to accomplish, but see if this does what you are wanting. I am using a ViewBox to stretch the Contents when the Window is resized, and setting the contraints in the Window. Be aware that Window Size is different than Window Client Area size.
<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" MaxHeight="500" MaxWidth="500"
MinWidth="100" MinHeight="100"
Width="200" Height="200">
<Viewbox StretchDirection="Both" Stretch="Fill">
<Button >Hello</Button>
</Viewbox>

Resize canvas to window / user control size

Hi I convert SVG image to XAML/canvas. I would like set this canvas as window/user control background.
Something like this:
<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">
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Name="svg2383" Width="800" Height="600">
<---->
</Canvas>
</Window>
My problem is Canvas is too much big, I would like automatic resize/stretch canvas on window height/width.
Image on canvas is complicated..http://kde-look.org/content/show.php/something_wall?content=115863
So set Width and Height is not solution, because this canvas consist other canvas.
How about removing specified width and height
<canvas Name="svg2383">
<---->
</canvas>
EDIT: Another way is to use a binding as follows:
<Window x:Name="MainWin">
<Canvas Width="{Binding ElementName=MainWin, Path=ActualWidth}"
Height="{Binding ElementName=MainWin, Path=ActualHeight}">
<--->
</Canvas>
</Window>
You can put the canvas within a Viewbox. This will allow you to stretch it to fill the window, while designing it at a constant size.

Is there any way we can Automatically Stretch Horizontally or Vertically Childrens in Canvas like Grid does?

Is there any way whether we can stretch Element placed inside a Convas if we give the Horizotal and Vertical Alignment properties to Stretch?
You could try something like this:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Canvas Name="theCanvas">
<Button Content="Hello" Width="{Binding ActualWidth, ElementName=theCanvas}" />
</Canvas>
</Window>

Enabling Scrollbar in WPF

I'm having a problem right now where my WPF application hides anything below the fold when the window is too small vertically. How can I use XAML to get a vertical scrollbar to appear for the entire app so the user can scroll to see the rest of the content?
Put a ScrollViewer inside your Window:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<ScrollViewer >
<!-- Window content here -->
</ScrollViewer>
</Window>

Resources