Basically WPF elements are rectangular in shape.
And it is very easy to get their outline through their Width and Height.
But in the general case, the contour of an element can be any line, including consisting of several contours that do not intersect with each other.
I expected that the VisualTreeHelper.GetClip () method would return the geometry of the contour.
But in my attempts, it always returns null.
Perhaps I am using it incorrectly, or it is not suitable for this purpose at all.
My attempt at getting the outlines:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace CopyGeometryElements
{
public partial class CopyGeometryElementsWindow : Window
{
public CopyGeometryElementsWindow()
{
InitializeComponent();
}
private void OnCopy(object sender, RoutedEventArgs e)
{
foreach (UIElement uie
in source.Children)
{
// Always returns null
var geometry = VisualTreeHelper.GetClip(uie);
var offset = uie.TranslatePoint(new Point(), source);
var copy = new Path()
{
Data=geometry,
Stroke = Brushes.Red,
StrokeThickness=2
};
Canvas.SetLeft(copy, offset.X);
Canvas.SetTop(copy, offset.Y);
target.Children.Add(copy);
}
}
}
}
<Window x:Class="CopyGeometryElements.CopyGeometryElementsWindow"
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:CopyGeometryElements"
mc:Ignorable="d"
Title="CopyGeometryElementsWindow" Height="450" Width="800">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid x:Name="source"
Margin="10" Background="White">
<Border BorderBrush="Green" BorderThickness="2"
CornerRadius="15"
Width="40" Height="60" Margin="10"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Ellipse Stroke="Green" StrokeThickness="2"
Width="40" Height="60"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="200,100,0,0"/>
<Polygon Stroke="Green" StrokeThickness="2"
HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="100,150,0,0"
Points="0,0 100,100 0,200 50,100">
</Polygon>
</Grid>
<Button Grid.Column="1" Padding="15 5"
HorizontalAlignment="Center" VerticalAlignment="Center"
Click="OnCopy">
<TextBlock Text="Copy the geometry of all shapes from the left panel to the right"
Width="80"
TextWrapping="Wrap"
TextAlignment="Center"/>
</Button>
<Canvas x:Name="target" Grid.Column="2" Background="White"
Margin="10"/>
</Grid>
</Window>
As im sure this code just getting ur Clip.
For Example:
<Button Height="35" Width="75">
<Button.Clip>
<GeometryGroup>
<RectangleGeometry Rect="0, 0, 100, 100"/>
</GeometryGroup>
</Button.Clip>
</Button>
In this case you can get this Clip.
var figure = VisualTreeHelper.GetClip(uie);
But your Clip is Empty, consequently your result will be null
Related
I have built a button wrapper around the User Control (FFU), so the object is clickable trough the main window. When the FFU object is clicked, I want to open another User Control: Popup FFU to be opened.
Main window XAML
<vw:BaseWindow
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="http://inosoft.com/visiwin7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:UserControls="clr-namespace:HMI.Windows.UserControls" x:Class="HMI.MainWindow"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="None" WindowState="Normal" WindowStartupLocation="CenterScreen"
Width="1024" Height="768">
<Grid x:Name="LayoutRoot" UseLayoutRounding="True">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<vw:Region x:Name="MainRegion" StartView="MainView1" DesignTimeView="MainView1" Grid.Row="0" Margin="0,96,0,4" Grid.RowSpan="2" />
<vw:Region x:Name="HeaderRegion" StartView="HeaderView" DesignTimeView="HeaderView" Grid.Row="0" />
<vw:Region x:Name="FooterRegion" StartView="FooterView" DesignTimeView="FooterView" Grid.Row="2" />
<UserControls:UC_FFU x:Name="FFU_1" HorizontalAlignment="Left" Height="28" Margin="305,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44"/>
<UserControls:UC_FFU x:Name="FFU_2" HorizontalAlignment="Left" Height="29" Margin="358,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44"/>
<UserControls:UC_FFU x:Name="FFU_3" HorizontalAlignment="Left" Height="29" Margin="410,85,0,0" Grid.Row="1" VerticalAlignment="Top" Width="44" StateBrush="{vw:VariableBinding VariableName=MCS1.Cleanroom.SIM_Cleanroom.SIM_FFUControl1.Observers.oStatus, Converter={StaticResource ValueToStateBrushConverter}, States={StaticResource BrushListStatus}, StateMode=Value}" PopupFFUInstance="{Binding MyDataContextPopupFFUProperty}"/>
</Grid>
</vw:BaseWindow>
UserControl code XAML
<UserControl x:Class="HMI.Windows.UserControls.UC_FFU"
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:HMI.Windows.UserControls"
mc:Ignorable="d"
x:Name="_this" d:DesignWidth="42" Height="27.544">
<Button x:Name="Btn" IsHitTestVisible="{Binding ElementName=Popup, Path=IsOpen, Mode=OneWay}" Click="Btn_Click">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="19*"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="{Binding ElementName=_this, Path=StateBrush}" Width="40" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,0,0" Grid.ColumnSpan="6"/>
<Line Stroke="#FF000000" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,0,0" Width="0" Y1="0" Y2="24" X1 ="-.5" X2="-.5"/>
<Line Stroke="#FF000000" Width="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin=".5,25,-2,0" Width="43" Y1="0" Y2="0.114682539682633" X2="40.5" Grid.ColumnSpan="6"/>
<Line Stroke="#FF000000" Height="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="1,1,-2,0" Width="43" Y1="0" Y2="0.114682539682633" X2="39.5" Grid.ColumnSpan="6"/>
</Grid>
<Button.Template>
<ControlTemplate x:Name="BlankButtonTemplate" TargetType="{x:Type ButtonBase}">
<Border Background="#00000000">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</UserControl>
UserControl FFU backhand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HMI.Windows.UserControls
{
/// <summary>
/// Interaction logic for UC_FFU.xaml
/// </summary>
public partial class UC_FFU : UserControl
{
//Create PopupFFUProperty
public static readonly DependencyProperty PopupFFUProperty =
DependencyProperty.Register("PopupFFUInstance", typeof(int), typeof(UC_FFU));
public int PopupFFUInstance
{
get { return (int)GetValue(PopupFFUProperty); }
set { SetValue(PopupFFUProperty, value); }
}
public UC_FFU()
{
InitializeComponent();
}
//Added the Click Event
public event RoutedEventHandler Click
{
add { Btn.AddHandler(ButtonBase.ClickEvent, value); }
remove { Btn.AddHandler(ButtonBase.ClickEvent, value); }
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
// Something to open the UserControl FFU Popup....
}
}
}
Simple Pop up example FFU XAML
<UserControl x:Class="HMI.Windows.UserControls.PopupControl.Popup_FFU"
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:HMI.Windows.UserControls.PopupControl"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50">
<Popup IsOpen="{Binding IsChecked, ElementName=button}" StaysOpen="False">
<Border Background="LightYellow">
<TextBlock>I'm the popup</TextBlock>
</Border>
</Popup>
</UserControl>
How can this be achieved?
I have tried to open a new window like this: however it should be a popup. Not a dialog window.
Window window = new Window
{
Title = "My User Control Dialog",
Content = new PopupControl.Popup_FFU()
};
window.Show();
This is what I would change in your code.
UC_FFU.xaml: Put this before your closing </Grid>
<Popup x:Name="ButtonPopup" StaysOpen="False">
<UserControls:Popup_FFU />
</Popup>
UC_FFU.xaml.cs:
private void Btn_Click(object sender, RoutedEventArgs e)
{
ButtonPopup.IsOpen = true;
}
Popup_FFU.xaml:
<UserControl x:Class="HMI.Windows.UserControls.PopupControl.Popup_FFU"
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:HMI.Windows.UserControls.PopupControl"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50">
<Border Background="LightYellow">
<TextBlock>I'm the popup</TextBlock>
</Border>
</UserControl>
I'm trying to create a custom popup window in WPF, but I can't seem to do it without a there being a black "drop shadow" around it. I'm using ContentControl for the body of it so that I can change the body for different popups.
I can remove the "shadow" by removing SizeToContent="WidthAndHeight", but then I don't have a window that adapts to it's content.
Here's the xaml
<Window x:Class="PriceFinding.Utility.Dialogs.Service.DialogWindow"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PriceFinding.Utility.Dialogs.Service"
mc:Ignorable="d"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterScreen"
MinHeight="300" MinWidth="800"
Background="{StaticResource BrushPrimary}"
>
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="50"/>
</WindowChrome.WindowChrome>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" WindowChrome.IsHitTestVisibleInChrome="True" VerticalAlignment="Top" Background="{StaticResource BrushPrimaryDark}" Name="TitleBar" Height="35">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Image Source = "pf.ico" Height="20" Width="20" Margin="5,0" />
<Label Content="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="14"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,5,0">
<Button
x:Name="MinButton"
Height="35" Width="35" Padding="0"
Command="{Binding MinimizeButton.MinimizeCommand}">
</Button>
<Button
x:Name="MaxButton"
Height="35" Width="35" Padding="0"
HorizontalAlignment="Left"
Command="{Binding MaximizeButton.MaximizeCommand}">
</Button>
<Button
x:Name="CloseButton"
Height="35" Width="35" Padding="0"
HorizontalAlignment="Right"
Command="{Binding CloseButton.CloseCommand}">
</Button>
</StackPanel>
</Grid>
<ContentControl Grid.Row="1" x:Name="ContentPresenter" Content="{Binding}"></ContentControl>
</Grid>
How can I get around this?
Here (or here, for the more daring) is a workaround!
This is quite a weird problem, but, indeed, running an event when the content is rendered:
ContentRendered="Window_OnContentRendered"
like so:
private void Window_OnContentRendered(object sender, EventArgs e)
{
InvalidateVisual();
}
InvalidateVisual does not remeasure content of window. Also no need to change the chrome.
This helped me:
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
// Content of window may be black in case of SizeToContent is set.
// This eliminates the problem.
// Do not use InvalidateVisual because it may implicitly break your markup.
InvalidateMeasure();
}
You can set the GlassFrameThickness to zero:
<WindowChrome GlassFrameThickness="0"
CaptionHeight="0" />
And that should remove the drop shadow effect and it will also allow you to use the CornerRadius effect if you were interested.
This is caused by a bug in WPF.
See https://social.msdn.microsoft.com/Forums/vstudio/en-US/f51ba061-69e2-4c85-b77a-41307423c2bf/sizetocontentheight-windowstylenone-resizemodenoresize-weirdness?forum=wpf
For a workaround, you can refresh the view. InvalidateVisual did not work for me.
You can attach an event handler to the window like so:
Background="{StaticResource BrushPrimary}" Activated="Window_Activated" StateChanged="Window_Activated"
Then, in the code behind:
private void Window_Activated(object sender, EventArgs e)
{
this.SizeToContent = SizeToContent.Manual;
this.SizeToContent = SizeToContent.WidthAndHeight;
}
I have a window with an Image that shows a Popup from its MouseEnter event handler:
private void theImage_MouseEnter(object sender, MouseEventArgs e)
{
contolPopup.IsOpen = true;
}
There is a button in this contolPopup that shows another Popup from its Click event handler:
private void setButton_Click(object sender, RoutedEventArgs e)
{
paramPopup.IsOpen = true;
}
This paramPopup behaves a bit strange just after it is shown: it does not close if I click somewhere outside it, but if I click on some control inside this paramPopup and then click somewhere outside, it closes fine.
What can cause this behavior?
I tried to focus paramPopup, but this does not help:
private void setButton_Click(object sender, RoutedEventArgs e)
{
paramPopup.IsOpen = setButton.IsChecked??false;
paramPopup.Focus();
}
Popup.StaysOpen is set to false for both Popups. I use .NET 4.0
Below I provided full source code:
fist popup is opened from a window:
<Window x:Class="GeoControls.WebcamWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GeoControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
SizeChanged="Window_SizeChanged" Loaded="Window_Loaded" SizeToContent="WidthAndHeight" ResizeMode="NoResize"
Title="{Binding Name}"
MouseEnter="theImage_MouseEnter" MouseLeave="theImage_MouseLeave" MouseLeftButtonUp="Window_MouseLeftButtonUp" Closed="Window_Closed">
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.ColumnSpan="2" Name="theImage" Width="{Binding CurrentResolution.Width}" Height="{Binding CurrentResolution.Height}" />
<TextBlock Name="waitingTextBlock" Grid.Row="0" Text="Waiting the response from TV Server..." Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Popup Name="contolPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=theImage}" PopupAnimation="Scroll" Placement="Left" AllowsTransparency="True">
<Border CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
<local:WebcamControl x:Name="webcamControl" Background="White">
</local:WebcamControl>
</Border>
</Popup>
</Grid>
</Window>
the second popup is opened by ParamButtons contained in WebcamControl contained in the first popup:
<UserControl x:Class="GeoControls.ParamButtons"
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"
xmlns:local="clr-namespace:GeoControls"
d:DesignHeight="30" d:DesignWidth="300">
<Grid>
............ omitted some code here................
<Popup Name="paramPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=setButton}" AllowsTransparency="True" Closed="paramPopup_Closed">
<Border Focusable="True" CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<local:ParamSlider Grid.Column="0" x:Name="paramSlider" />
<Button Content="OK" Click="okButton_Click" Grid.Column="1" Margin="5" />
<Button Content="Cancel" Click="cancelButton_Click" Grid.Column="2" Margin="5" />
</Grid>
</Border>
</Popup>
</Grid>
</UserControl>
I have a XAML file. In this XAML file I have a ScrollViewer to which later I add WinFormsHost -> basically place where you can put standard WinForm. When I scroll the ScrollViewer down the graphics from inside of it goes outside. Please look at the first an the second screenshot. How to deal with that drawing problem? Can I set ZIndex for that ScrollViewer?
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Example"
Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="3*" />
<RowDefinition Height="7*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" /> <!-- this column is for future purpouses -->
<ColumnDefinition Width="1366" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border BorderBrush="Red" BorderThickness="4" Grid.Column="0" Grid.Row="0">
<ListBox x:Name="ListBox"
</ListBox>
</Border>
<Button x:Name="AcceptButton" Grid.Row="1" ></Button>
</Grid>
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
<ScrollViewer Grid.Column="1" Grid.Row="2" >
<WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>
</Grid>
</Window>
AT THE BEGINNING
SCROLLED DOWN - WHEN SCROLLED UP EVERYTHING GOES BACK TO NORMAL
**EDIT ** Tried, but no difference:
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2" ClipToBounds="True">
<ScrollViewer ClipToBounds="True" Grid.Column="1" Grid.Row="2" >
<WrapPanel ClipToBounds="True" x:Name="VideoPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>
EDIT 2:
XAML
<Window x:Class="HomeSecurity.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HomeSecurity"
Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
<ScrollViewer >
<WrapPanel x:Name="VideoPanel" >
</WrapPanel>
</ScrollViewer>
</Border>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
MAINWINDOW.XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HomeSecurity {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
add(); add(); add();
}
private void add() {
WindowsFormsHost formsHost = new WindowsFormsHost();
VideoStream videoStream = new VideoStream();
formsHost.Width = 400;
formsHost.Height = 400;
formsHost.Child = videoStream;
Border lineBorder = new Border();
lineBorder.BorderBrush = Brushes.Green;
lineBorder.BorderThickness = new Thickness(2);
lineBorder.Child = formsHost;
VideoPanel.Children.Add(lineBorder);
}
}
}
There has to be added reference to System.WindowsFormsIntegration, System.Windows.Forms, System.Drawing.
You clearly have some problem there and you should thoroughly investigate what is going on with your code to try to rectify the situation optimally. However, the code that you have shown here is not to blame. I tried it in a new project and it works as expected. As there is nothing inside the WrapPanel, there is no content in the ScrollViewer to cause your problem. When adding code to demonstrate a problem, you should ensure that your code does demonstrate the problem.
Even so, without being able to recreate your problem, there might just be a quick fix that you can use in this case. You can just set the UIElement.ClipToBounds property on the Border to True and this will clip anything that extends beyond its bounds.
<Border ClipToBounds="True" BorderBrush="Green" BorderThickness="2" ...>
<ScrollViewer Grid.Column="1" Grid.Row="2" >
<WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
</WrapPanel>
</ScrollViewer>
</Border>
I think I am trying to do something relatively simple in WPF, but can't for the life of me figure out how; and think I am probably on the verge of overcomplicating it.
If I had a grid which was 3 rows and 3 columns, and I wanted to join the corners of two cells to create a diagonal border, what would be the best way of doing so?
The lines should ideally re-size if the control is resized (so bound to the corners of the cell?).
Essentially I would like to create the red lines in the diagram hosted here: Example Pic http://imm.io/7A4L
You could use a Path with Stretch=Fill. For the top right cell in your example, you would use:
<Path Grid.Row="2" Grid.Column="0" Stroke="Red" StrokeThickness="2" Stretch="Fill">
<Path.Data>
<LineGeometry StartPoint="0,0" EndPoint="1,1" />
</Path.Data>
</Path>
The "Fill" stretch makes the Path stretch to fill its parent, which gives the impression that the coordinates of the LineGeometry are relative (X=0,Y=0 is top left, X=1,Y=1 is bottom right).
I have created a sample to draw line from code behind which will give you more control...
I crated a grid which contains canvas in each cell and on canvas load i am creating a path and adding it to same canvas...
As # Mathieu Garstecki answer we can achieve this crating path in xaml... if want to add some logic before creating path you can use my answer
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Canvas Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="2" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="0" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="1" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
<Canvas Grid.Row="2" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" Loaded="Canvas_Loaded"></Canvas>
</Grid>
Code Behind
private void Canvas_Loaded(object sender, RoutedEventArgs e)
{
var g = new StreamGeometry();
var context = g.Open();
context.BeginFigure(new Point(0, 0), true, true);
context.LineTo(new Point((sender as Canvas).ActualHeight, (sender as Canvas).ActualWidth), true, true);
context.Close();
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
path.Data = g;
path.Stroke = new SolidColorBrush(Colors.Red);
path.StrokeThickness = 1.4;
(sender as Canvas).Children.Add(path);
}
OutPut