Creating controls - silverlight

I am designing a silverlight application in which i have a image control in the left top corner and when i click the button and drag it to the form i should get duplicate of that control and dragged into my form.
Please help me with the code
I am trying to create the control dynamically in mouseleftbuttondown event but the controls are not being created.
Xaml
<UserControl xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="Workflow.MainPage"
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" d:DesignWidth="640" d:DesignHeight="480">
<Canvas x:Name="layout" Width="800" Height="600" Background="AliceBlue">
<Image x:Name="MyImage" Source="21.jpg" Canvas.Left="10" Canvas.Top="10" Stretch="Uniform"
MouseLeftButtonDown="MyImage_MouseLeftButtonDown" ></Image>
</Canvas>
</UserControl>
Code
private void MyImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
b = cs.LoadControl();
layout.Children.Add(b);
}
List<Ellipse> block = new List<Ellipse>();
public Ellipse LoadControl()
{
Ellipse btn = new Ellipse();
block.Add(btn);
btn.Height = 50; btn.Width = 100;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(0, 255, 255, 0);
btn.Fill = mySolidColorBrush;
Canvas.SetTop(btn, 50);
Canvas.SetLeft(btn, 50);
return btn;
}

You are using Color.FromArgb(0, 255, 255, 0) which has 0 for alpha, which makes your control transparent. What if you try Color.FromArgb(255, 255, 255, 0)?

Related

Wpf screen edge swipe detect

I have a basic wpf app that basically detects a touch input and moves a rectangle according to the input. My problem is that if that touch comes outside the screen(For example: I touch the bottom bezel of the screen and slide it in the touch area) it won't be detected. Is there a way to get over this problem? I want to detect edge swipes as well.
XAML:
<Window x:Class="touch_test.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:touch_test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Stylus.IsPressAndHoldEnabled="False"
>
<Canvas>
<Rectangle Width="50" Height="50" x:Name="rect" Fill="Red"/>
</Canvas>
</Window>
Cs:
protected TouchPoint TouchStart;
public MainWindow()
{
InitializeComponent();
this.PreviewTouchDown += new EventHandler<TouchEventArgs>(BasePage_TouchDown);
this.PreviewTouchMove += new EventHandler<TouchEventArgs>(BasePage_TouchMove);
WindowState = WindowState.Maximized;
WindowStyle = WindowStyle.None;
}
void BasePage_TouchDown(object sender, TouchEventArgs e)
{
TouchStart = e.GetTouchPoint(this);
}
void BasePage_TouchMove(object sender, TouchEventArgs e)
{
Rectangle rectToMove = rect;
rectToMove.RenderTransform = new MatrixTransform();
Matrix matr = ((MatrixTransform)rectToMove.RenderTransform).Matrix;
var currentTouchPoint = e.GetTouchPoint(this);
matr.Translate(currentTouchPoint.Position.X, currentTouchPoint.Position.Y );
rectToMove.RenderTransform = new MatrixTransform(matr);
}

WPF Screen Capture doesn't include title bar

I implemented the code in the first answer to this question and it works great except that it doesn't include the title bar, which is a must for my needs. Does anybody have any idea what I'm doing wrong?
I've updated my example to show all the other commented out attempts that failed.
Here's my code:
Window shellView = Application.Current.MainWindow;
Rect bounds = VisualTreeHelper.GetDescendantBounds(shellView);
//Rect bounds = new Rect(new Size(shellView.ActualWidth, shellView.ActualHeight));
//Rect bounds = shellView.RestoreBounds;
//Rect bounds = VisualTreeHelper.GetContentBounds(shellView);
//Rect bounds = new Rect(new Size(VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualWidth,
// VisualTreeHelperEx.FindDescendantByType<Window>(shellView).ActualHeight));
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
string fileName = $#"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\Screen_Capture.png";
if (_dialogService.ShowSaveFileDialog(ref fileName, "PNG Files | *.png"))
{
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(shellView);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}
renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(fileName))
{
bitmapEncoder.Save(stm);
}
}
And at XAMIMAX's request, what I can share of the XAML with names changed:
<Window x:Class="MyProject.Shell.Views.Shell.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ap="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
xmlns:controls="clr-namespace:MyProject.Common.Support.Controls;assembly=MyProject.Common.Support"
xmlns:converters="clr-namespace:MyProject.Common.Support.Converters;assembly=MyProject.Common.Support"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dts="clr-namespace:MyProject.Common.Support.DataTemplateSelectors;assembly=MyProject.Common.Support"
xmlns:enums="clr-namespace:MyProject.Shell.Enums"
xmlns:enums1="clr-namespace:MyProject.Common.Support.Enums;assembly=MyProject.Common.Support"
xmlns:ikriv="clr-namespace:MyProject.Common.Support.AttachedProperties;assembly=MyProject.Common.Support"
xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:m="clr-namespace:MyProject.Common.Support.MarkupExtensions;assembly=MyProject.Common.Support"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resources="clr-namespace:MyProject.Shell.Views.Shell.Resources"
xmlns:ss="clr-namespace:MyProject.Common.Support.StyleSelectors;assembly=MyProject.Common.Support"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:views="clr-namespace:MyProject.NotificationModule.Client.Views;assembly=MyProject.NotificationModule.Client"
xmlns:constants="clr-namespace:MyProject.Shell"
x:Name="shell"
Title="{Binding MyProjectApplicationTitle}"
Width="1024"
Height="768"
MinWidth="800"
MinHeight="600"
Background="{DynamicResource PrimarySolidColorBrush}"
Icon="/Resources/Embedded/MyProject.ico"
mc:Ignorable="d">
<controls:LocalizationScope.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.Children>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NavigationBarWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MinWidth="38"
MaxWidth="400" />
</Grid>
</Grid.Children>
</Grid>
</controls:LocalizationScope.Content>
</Window>
I hope that's enough of the XAML to help. I also tried the example at this link Generating a screenshot of a WPF window, but got the same results: no title bar.
Thanks to the link that #Clemens sent me to, I was able to use some of the code on that page to come up with this working method. It grabs a few pixels more that the active window, but that works for me!
private void TakeScreenshot()
{
var rect = new Rect();
GetWindowRect(GetForegroundWindow(), ref rect);
var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), System.Drawing.Point.Empty, bounds.Size);
}
string fileName = $#"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\Screen_Capture.png";
if (_dialogService.ShowSaveFileDialog(ref fileName, "PNG Files | *.png"))
{
result.Save(fileName, ImageFormat.Png);
_dialogService.ShowInfoDialog(_localizationService["Shell.Views.Shell.HelpMenu.ScreenshotSaved"] + $"{fileName}");
}
}

WPF: Overlapping animations - move a grid behind a toolbar

I want to move a window within the main window in wpf. In the example below I want to move the window "moving window" to the top of the main window until it is completely off the main window. I do this with a TranslateTransform.Y animation - the moving window is actually a grid. The problem is, that I want to move the window behind the toolbar. Currently if is moved over the toolbar. In other words: I want the moving window to disappear behind the toolbar. The toolbar is part of a dock panel, which has a gradient background, the toolbars background is transparent. It doesn't matter what ZIndex I use, the moving window (grid) always moves over the toolbar and is always only cut off by the windows client area (so it "disappears" behind the windows titlebar). How can I achieve such an animation?
-------------------------------------------
| Toolbar |
-------------------------------------------
| ------------------- |
| | moving window | |
| ------------------- |
| |
-------------------------------------------
I created a little quick and dirty Non-MVVM Demo Project to make it clearer. This is the main Window.xaml:
<Window x:Class="MovingWindowTest.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 Background="SlateGray">
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Vertical">
<Menu IsMainMenu="True" Background="Transparent" Panel.ZIndex="2">
<Menu.ItemsPanel>
<ItemsPanelTemplate>
<DockPanel HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</Menu.ItemsPanel>
<MenuItem Header="Debug">
<MenuItem Header="Move window in" Click="MoveWindowIn_OnClick" />
<MenuItem Header="Move window out" Click="MoveWindowOut_OnClick" />
</MenuItem>
</Menu>
</StackPanel>
<Grid x:Name="contentGrid" Background="White"/>
</DockPanel>
</Grid>
The Code behind:
public partial class MainWindow : Window
{
private InnerWindow _InnerWindow;
private InnerWindow InnerWindow
{
get { return this._InnerWindow ?? ( this._InnerWindow = new InnerWindow() ); }
}
public MainWindow()
{
InitializeComponent();
this.InnerWindow.Width = this.InnerWindow.Height = 100;
}
private void MoveWindowIn_OnClick( object sender, RoutedEventArgs e )
{
Panel.SetZIndex( this.contentGrid, 1 );
this.InnerWindow.Visibility = Visibility.Visible;
this.contentGrid.Children.Add( this.InnerWindow );
TranslateTransform trans = new TranslateTransform();
DoubleAnimation anim = new DoubleAnimation(-100, 0, new Duration( new TimeSpan( 0, 0, 0, 3 ) ) );
this.InnerWindow.RenderTransform = trans;
trans.BeginAnimation( TranslateTransform.YProperty, anim );
}
private void MoveWindowOut_OnClick( object sender, RoutedEventArgs e )
{
TranslateTransform trans = new TranslateTransform();
DoubleAnimation anim = new DoubleAnimation( 0, -100, new Duration( new TimeSpan( 0, 0, 0, 3 ) ) );
this.InnerWindow.RenderTransform = trans;
anim.Completed += ( o, args ) => this.contentGrid.Children.Remove( this.InnerWindow ); ;
trans.BeginAnimation( TranslateTransform.YProperty, anim );
}
}
The InnerWindow is just a super simple User Control with the following xaml:
<UserControl x:Class="MovingWindowTest.InnerWindow"
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" Background="Red" VerticalAlignment="Top"
d:DesignHeight="100" d:DesignWidth="100">
<Grid>
<TextBlock FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center">TEST</TextBlock>
</Grid>
When the InnderWindow is moved in, it overlaps the Menu, although the menu has a higher ZIndex.
Note that the InnerWindow is added to the contentGrid which is under the menu. And it makes no difference when I add ClipToBounds to the inner window.
What is the parent of the moving window? The DockPanel that also contains the Toolbar or another control? Have you tried setting the ClipToBounds property on the moving window?

How to set multiple canvas in scroll viewer in wpf?

I want to set an image in a canvas control and have 20 canvases that I am creating by using a loop. The problem is that when I want to add all those canvas items in to a scroll viewer it does not work. Here is my code:
private void CreateAndShowCanvas()
{
List<Canvas> list = new List<Canvas>();
for (int i = 0; i < 20; i++)
{
Canvas myCanvas1 = new Canvas();
myCanvas1.Background = new SolidColorBrush(Colors.Transparent);
myCanvas1.Height = 235;
myCanvas1.Width = 626;
//Canvas.SetZIndex(myCanvas1, 4);
Image MainImage = new Image();
MainImage.Width = 275;
MainImage.Height = 235;
BitmapImage mi = new BitmapImage(new Uri("select_1.png", UriKind.Relative));
MainImage.Source = mi;
Canvas.SetTop(MainImage, 0);
Canvas.SetLeft(MainImage, 0);
myCanvas1.Children.Add(MainImage);
Image SeparatorImage = new Image();
BitmapImage si = new BitmapImage(new Uri("Sentre Seprator.png", UriKind.Relative));
SeparatorImage.Height = 270;
SeparatorImage.Source = si;
Canvas.SetTop(SeparatorImage, -5);
Canvas.SetLeft(SeparatorImage, 310);
myCanvas1.Children.Add(SeparatorImage);
Image SecondImage = new Image();
SecondImage.Width = 275;
SecondImage.Height = 235;
BitmapImage sci = new BitmapImage(new Uri("select_2.png", UriKind.Relative));
SecondImage.Source = sci;
Canvas.SetTop(SecondImage, 0);
Canvas.SetLeft(SecondImage, 350);
myCanvas1.Children.Add(SecondImage);
list.Add(myCanvas1);
}
scv.Content = list;
}
and in XML:
<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" Height="350" Width="525">
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent" />
</Grid>
</Window>
and when I run it, it only shows ("Collection"). Please help me out, thanks in advance...
Should you not just be using a ListBox with an ItemTemplate that has Canvases inside it instead? There must be an easier way to do whatever you are trying to achieve than creating 20 canvases manually.
Here's some reading to do with ListBoxes.
A ScrollViewer can hold just a single child, so you need another panel type like a Grid or StackPanel which holds your canvases.
<Grid>
<ScrollViewer Name="scv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="60,40,59,46" Opacity="99" Background="Transparent">
<StackPanel Name="stp" />
</ScrollViewer>
</Grid>
Add canvases to stp instead.

Fullscreen richtextbox in silverlight

I have a dataform with a richtextbox In it. The user can type some text and have some editing capability, but I'd like to give the user the option to expand the editor to fullscreen to have more richtextbox editing options. How can I implement a function that will allow me to fullscreen (or atleast create a bigger window) the richtexteditor so the user has better overview over the document and more editing options?
This is ment to be possible in OOB mode.
Full screen wont work as you have limit keyboard input in fullscreen:
Up Arrow
Down Arrow
Left Arrow
Right Arrow
Spacebar
Tab
Page Up
Page Down
Home
End
Enter
What you can do is for example is make your element fill the whole space of your silverlight application by making it the exact size of your RootVisual and adjusting your margins to place it correctly in your application:
XAML:
<UserControl x:Class="SilverlightApplication1.MyRichTextBox"
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button x:Name="FullScreen" Grid.Row="0" Content="FullScreen" Click="FullScreen_Click" />
<RichTextBox Grid.Row="1" />
</Grid>
Code-behind:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace SilverlightApplication1
{
public partial class MyRichTextBox : UserControl
{
private Thickness _oldMargin;
private double _oldHeight = double.NaN;
private double _oldWidth = double.NaN;
private HorizontalAlignment _oldHorizontalAlignment;
private VerticalAlignment _oldVerticalAlignment;
private bool _fullScreen = false;
public MyRichTextBox()
{
InitializeComponent();
}
private void FullScreen_Click(object sender, RoutedEventArgs e)
{
if (_fullScreen)
{
_fullScreen = false;
Margin = _oldMargin;
Width = _oldWidth;
Height = _oldHeight;
}
else
{
_fullScreen = true;
_oldMargin = Margin;
_oldWidth = Width;
_oldHeight = Height;
_oldHorizontalAlignment = HorizontalAlignment;
_oldVerticalAlignment = VerticalAlignment;
FrameworkElement rootVisual = Application.Current.RootVisual as FrameworkElement;
GeneralTransform generalTransform = TransformToVisual(rootVisual);
Point position = generalTransform.Transform(new Point(0, 0));
Width = rootVisual.ActualWidth;
Height =rootVisual.ActualHeight;
Margin = new Thickness(-position.X - 1, -position.Y - 1
, (ActualWidth + position.X) - rootVisual.ActualWidth - 1
, (ActualHeight + position.Y) - rootVisual.ActualHeight - 1);
}
}
}
}

Resources