Moving a triangle using given coordinate - c#

I'm working on a project and I want to animate a rectangle to move a certain amount of coordinate that i have set earlier.
Ex: My rectangle is at the position (x=0,y=0). I want with a click of a button to make it move at position (x=150, y=230) in interval of 100 milliseconds. So with one click, it would go to (10,25) at first 100 milliseconds,(20,35) for second 100 milliseconds and so on until the rectangle reach the final position(x=150, y=230)...
I'm working with visual studio and blend on Visual C# wpf.
This is my progress of codes in code-behind file:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myRect = (Rectangle)this.FindName("myRect");
double x = Canvas.GetLeft(myRect);
double y = Canvas.GetTop(myRect);
Canvas.SetLeft(myRect,x+10);
Canvas.SetTop(myRect,y);
And this is in XAML code:
<Window x:Class="move.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:move"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Border BorderThickness="1" BorderBrush="Aqua">
<Canvas Name="PointCanvas" Width="500" Height="294" Margin="9,0,6,0">
<Rectangle x:Name="myRect" Fill="#FFF5F4F5" Height="39" Canvas.Left="170" Stroke="Black" Canvas.Top="89" Width="89"/>
</Canvas>
</Border>
<Button Name="Move" Click="Button_Click">Move</Button>
</StackPanel>

Related

How to add some of my code to xaml property ResizeMode= CanResizeWithGrip or whenever it is called

Hi at the begining of this question i want to say that i'm beginer and thing i've done here could prolly be done better but i'm still learnig. i want to set a value of some variable that contains the actual width of grid, and whenever i resize my window obviously this width change and I don't know how to access it. What i want to do ? Just want my progress bar to scale up or down when resizing
Here is some code
<Window x:Class="Lista6.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"
xmlns:local="clr-namespace:Lista6"
mc:Ignorable="d"
Title="MainPage" Height="650" Width="1000"
Background="#36393F"
ResizeMode="CanResizeWithGrip"
Icon="/img/Ikonka2.png"
WindowStyle="None">
///other xaml code
<ProgressBar Foreground="White"
Value="50"
BorderThickness="0"
Background="Gray"
Height="5"
Width="{Binding Path=ProgresBarWidth[0], Mode=OneWay}"/>
</StackPanel>
</Grid>
</Grid>
</Window>
ObservableCollection<double> width = new ObservableCollection<double> { };
public ObservableCollection<double> ProgresBarWidth
{
get
{
return width;
}
set
{
width = value;
}
}
public AppOperator()
{
width.Add(0);
}
private void ProgresbarWidthSetter()
{
MainPage mp = new MainPage();
width[0] = mp.MusicBar.Width;
}
Here as u can see i ve binded progres bar width to ObservColl property, and i want to call it when im resizing my window but i ve no idea how to do that.
Thanks for any help and wish u all good.

Need help to refresh a brush of a visual child object (a Border) within viewport2DVisual3D object class

I am using the Planerator control which works great. The only problem is that the brush of the Border object I added inside it doesn't get changed. It rotates well, but the brush seems frozen. I tried many things, but no can do.
Control link: https://github.com/pauldotknopf/WPF-MediaKit/blob/master/SampleApplication/Controls/Planerator.cs
I search a lot on many help sites, including stackoverflow. Here is what I tried without any success;
Important: I am working from code behind, not XAML.It worked fine in XAML
this.UpdateLayout();
this.InvalidateMeasure();
this.InvalidateVisual();
this.InvalidateArrange();
((UIElement)_originalChild).UpdateLayout();
var child = VisualTreeHelper.GetChild(viewport2DVisual3D, 0);
((UIElement)child).UpdateLayout();
((UIElement)child).InvalidateMeasure();
((UIElement)child).InvalidateVisual();
((UIElement)child).InvalidateArrange();
From this code;
// Interactive frontside Visual3D
Viewport2DVisual3D frontModel = new Viewport2DVisual3D() { Geometry = simpleQuad, Visual = _logicalChild, Material = frontMaterial, Transform = xfGroup };
Now, after testing, I am suspecting that once the object is added inside Viewport2DVisual3D has a child, color & brushes don't get refreshed anymore, but it will until/before that point. See "Visual = _logicalChild" above.
I have no error message, it rotates well, but the brush and color of my border don't get updated if I change the color.
Here is my working markup;
<Window
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:TestPlaneratorInXAML"
xmlns:Planerator="clr-namespace:Planerator;assembly=Planerator" x:Class="TestPlaneratorInXAML.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Planerator:Planerator x:Name="aPlaneratorControl" Height="100" VerticalAlignment="Top" Width="100">
<Border BorderBrush="Black" BorderThickness="5" HorizontalAlignment="Center" Height="100" VerticalAlignment="Top" Width="100" CornerRadius="5" Background="Black"/>
</Planerator:Planerator>
<Button x:Name="ChangeBackground" Content="Change background" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160" PreviewMouseDown="ChangeBackground_PreviewMouseDown"/>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace TestPlaneratorInXAML
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ChangeBackground_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
((Border)aPlaneratorControl.Child).Background = Brushes.Red;
}
}
}

How can I retrieve coordinates on a mouse click with ViewportPointToLocation?

I'm using Bing map WPF control SDK to try retrieving coordinates and printing them
I've managed to retrieve the coordinates of the center of the current LocationRect using an EventHandler associated with pressing the arrows
I've tried employing the same concept with mouse clicking event handlers but it didn't work, first I've registered the event using the += notation as follows:
public MainWindow()
{
InitializeComponent();
MainMap.Mode = new AerialMode(true);
MainMap.Focus();
MainMap.Culture = "ar-sa";
MainMap.MouseDoubleClick += new MouseButtonEventHandler(MapWithPushpins_MouseDoubleClick);
}
private void MapWithPushpins_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Point mousePosition = e.GetPosition(this);
Location pinLocation = MainMap.ViewportPointToLocation(mousePosition);
Pushpin pin = new Pushpin();
pin.Location = pinLocation;
Coordinates.Text = pinLocation.Longitude.ToString();
MainMap.Children.Add(pin);
}
And here's the XAML file:
<Window x:Class="Ornina.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:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:local="clr-namespace:Ornina"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid VerticalAlignment="Top" >
<m:Map CredentialsProvider="AqCitpgSjIz_Sxd6AyI9Zm1rs1uRSG_G3Y7ebfok69ufB8W8uRdUtvheaRbz_10t" x:Name="MainMap" Center="36,38" ZoomLevel="16" Mode="AerialWithLabels" HorizontalAlignment="Center" VerticalAlignment="Top" Height="300" Width="500">
</m:Map>
</Grid>
<TextBlock x:Name="Coordinates">Coordinations</TextBlock>
</Grid>
</Window>
The program isn't responding with any thing, no exceptions, no errors
Your TextBlock is in front of your map, so the map doesn't receive the MouseDoubleClick event.
You can change the TextBlock to:
<TextBlock x:Name="Coordinates"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="Coordinations" />
So it's only in the top left corner and not in front of the whole map.
Or you can move it outside of the map entirely, in a different grid row or column.

C# WPF - Popup which doesn't get focused

Im simply trying to make a little popup which shows a message in the corner. This Popup shuld be at the top of every other Window which I achieved with "TopMost", but I can't seem to get the unfocusable thing to work...
My PopUp XAML:
<Window x:Class="message.Popup"
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:message"
mc:Ignorable="d"
Title="Popup" Height="129.808" Width="300" Focusable="False" Topmost="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="179*"/>
<ColumnDefinition Width="113*"/>
</Grid.ColumnDefinitions>
<Label x:Name="label" Content="" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top" Width="272" Grid.ColumnSpan="2"/>
</Grid>
How I call it (from annother Window):
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
new Popup(textBox.Text).Show();
}
PopUp code:
public Popup(string text)
{
InitializeComponent();
label.Content = text;
}
you must define your Mainwindow as Owner of the Popup and center the StartupLocation Property. Something like this:
PopUpWindow.Owner = MainWindow;
PopUpWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
Or if you are calling it from another window:
PopUpWindow.Owner = this;
PopUpWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
However I must say: this is not MVVM, since you are storing text in the Window class, and I strongly recommend you start reading about MVVM.

VtkActor is not displayed on WPF project

I am trying to load a RenderWindowControl from vtk libraries on my WPF project using ActiViz.NET and Visual Studio 2013. The library works fine since I did a new project just to practice on it but when I try to integrate it into my work, the actor is not shown. This is my code:
MainWindow.xaml:
<Window x:Class="myProject.Views.MainWindow"
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:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
x:Name="Mainwindow"
MinHeight="600"
MinWidth="800"
Title="{Binding Title}"
Height="720"
Width="1280"
Icon="{StaticResource ApplicationIcon}"
Loaded="OnLoaded"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
Style="{StaticResource WindowStyle}"
mc:Ignorable="d">
<DockPanel>
<TabControl>
....
....
<VtkTab:VtkTabView />
....
....
</TabControl>
</DockPanel>
</Window>
VtkTabView.xaml:
<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
Height="480" Width="640">
<WindowsFormsHost Name="Wfh">
<vtk:RenderWindowControl x:Name="RenderControl" />
</WindowsFormsHost>
</UserControl>
VtkTabView.xaml.cs:
public partial class UITabView
{
protected static Random _random = new Random();
vtkActor actor = vtkActor.New();
public VtkTabView()
{
InitializeComponent();
var sphere = vtkSphereSource.New();
sphere.SetThetaResolution(8);
sphere.SetPhiResolution(16);
var shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphere.GetOutputPort());
shrink.SetShrinkFactor(0.9);
var move = vtkTransform.New();
move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
var moveFilter = vtkTransformPolyDataFilter.New();
moveFilter.SetTransform(move);
moveFilter.SetInputConnection(shrink.GetOutputPort());
var mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(moveFilter.GetOutputPort());
// The actor links the data pipeline to the rendering subsystem
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
RenderControl.Load += RenderWindowControlOnLoad;
}
void RenderWindowControlOnLoad(object sender, EventArgs e)
{
var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
renderer.SetBackground(0, 1, 0);
renderer.Render();
}
}
As you can see, in the method RenderWindowControlOnLoad (VtkTabView.xaml.cs) I take the actor (a Sphere) and I add it to the render declared into the WPF but it is not displayed. I tried to change the background color and call to render just in case but it's still not working :(
Call
RenderControl.RenderWindow.Render();
instead of
renderer.Render();

Categories