move canvas on key down - wpf

I want to move canvas right when I press ->.
I set event KeyDown and this is method for event
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key==Key.Right)
{
}
}
But what to write inside if?Canvas.setLeft doesn't work
<Canvas x:Name="totle" KeyDown="Window_KeyDown">
<Ellipse Name="yio" Canvas.Left="40" Canvas.Top="40" Height="30" Width="30" Fill="Beige"/>
<Line X1="40" Canvas.Left="67" Canvas.Top="51" StrokeThickness="40" Stroke="Red" Height="10" Width="45" Fill="#FFD86464" OpacityMask="Red" />
<Rectangle Canvas.Left="20" Canvas.Top="70" Width="70" Height="20" Fill="Beige"/>
</Canvas>

To achieve this effect, first use a RenderTransform and name it canvasTranform:
<Canvas x:Name="totle" KeyDown="Window_KeyDown">
<Canvas.RenderTransform>
<TranslateTransform x:Name="canvasTransform" />
</Canvas.RenderTransform>
<Ellipse Name="yio" Canvas.Left="40" Canvas.Top="40" Height="30" Width="30" Fill="Beige"/>
<Line X1="40" Canvas.Left="67" Canvas.Top="51" StrokeThickness="40" Stroke="Red" Height="10" Width="45" Fill="#FFD86464" OpacityMask="Red" />
<Rectangle Canvas.Left="20" Canvas.Top="70" Width="70" Height="20" Fill="Beige"/>
</Canvas>
Then, in your event handler, reference the TranslateTransform by name and set the X property:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
canvasTransform.X = 100;
}
}
If you the transform to be additive (the canvas keeps moving right as you press the right arrow), add to the X property each time:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
canvasTransform.X += 100;
}
}

Related

Change image after button click using MahApps Metro

I am using MahApps Metro anf this is my Button:
<Button
Name="startBtn"
Width="55"
Height="55"
VerticalAlignment="Top"
Style="{DynamicResource MetroCircleButtonStyle}"
Click="playBtn_Click">
<Rectangle
Width="25"
Height="25">
<Rectangle.Fill>
<ImageBrush
ImageSource="pack://application:,,,/Resources/start.ico" />
</Rectangle.Fill>
</Rectangle>
</Button>
So all i want is change the Button image inside my Button Click Event:
private void startBtn_Click(object sender, RoutedEventArgs e)
{
}
XAML:
<Button
Name="startBtn"
Width="55"
Height="55"
VerticalAlignment="Top"
Style="{DynamicResource MetroCircleButtonStyle}"
Click="playBtn_Click">
<Image Width="25" Height="25" Source="pack://application:,,,/Resources/start.ico" />
</Button>
Code:
private void startBtn_Click(object sender, RoutedEventArgs e)
{
((sender as Button).Content as Image).Source = new BitmapImage(new Uri("yourpath.png"));
}

How control re-position depends on other control visibility in same panel

I have two buttons inside a stack panel. Initially B1 button is on top, then B2. I will change button visibility dynamically from code so that, when I change B1 visibility hidden, then B2 will come on top. How can I achieve this functionality.
<Grid>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
</StackPanel>
</Grid>
First you remove the Statckpanel and put then in a Grid and you can achieve
Try something like this.
<Grid>
<Button Content="B1" Height="20" Width="100" Visibility="Visible" Click="Button_Click" x:Name="B1" />
<Button Content="B2" Height="20" Width="100" Visibility="Visible" x:Name="B2" Click="B2_Click" />
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
B1.Visibility = System.Windows.Visibility.Hidden;
B2.Visibility = System.Windows.Visibility.Visible;
}
private void B2_Click(object sender, RoutedEventArgs e)
{
B2.Visibility = System.Windows.Visibility.Hidden;
B1.Visibility = System.Windows.Visibility.Visible;
}
This should give you similar behaviour. Change according to your use

Silverlight CallMethodAction

How to get this CallMethodAction method InitBlah? Currently it doesn't fire. Am suspecting it the signature that is wrong:
<Grid x:Name="LayoutRoot">
<Button x:Name="btnEnter" Content="Enter" HorizontalAlignment="Left" Height="48" Margin="96,88,0,0" VerticalAlignment="Top" Width="144">
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="MouseEnter" SourceName="btnEnter">
<interactions:ChangePropertyAction PropertyName="Visibility" Value="Visible" TargetName="TextBoxTest" />
<interactions:CallMethodAction MethodName="InitBlah" TargetObject="{Binding}"/>
</interactivity:EventTrigger>
<interactivity:EventTrigger EventName="MouseLeave" SourceName="btnEnter">
<interactions:ChangePropertyAction PropertyName="Visibility" Value="Collapsed" TargetName="TextBoxTest" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</Button>
<Grid>
<TextBlock Name="TextBoxTest" Width="100" Height="100" Visibility="Collapsed">Hello</TextBlock>
code behind:
public void InitBlah(object sender, RoutedEventArgs e)
{
var x = 1;
}
The ChangePropertyAction works fine: when I hover over the button, the textbox pops up saying hello.
The signature of your InitBlah method should be matched for appropriate evets.
Try this:
public void InitBlah(object sender, MouseEventArgs e)
{
var x = 1;
}

Change object's layer dynamically in WPF

I need to do something like "Move up", "Move down" with the objects on my GRID from C# code while executing, is there any possibilities?
You can try this code:
private bool _isUp = false;
private void button1_Click(object sender, RoutedEventArgs e) {
if (_isUp) {
Canvas.SetZIndex(rectangle1, 1);
} else {
Canvas.SetZIndex(rectangle1, 0);
}
_isUp = !_isUp;
}
I just use 2 rectangles in my sample, for this.
<Rectangle Height="100" HorizontalAlignment="Left" Margin="68,142,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF9D2A2A" />
<Rectangle Height="100" HorizontalAlignment="Left" Margin="10,120,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF265D80" />

Changing RenderTransformOrigin property of rotated object

how can I make a rotated object stand still when its RenderTransformOrigin property is changed? After running the code below, you can notice by rotating the object (the slider does that), changing the origin of rotation (the button) and rotating it again, that the object slightly moves. What I'm trying to achieve is being able to rotate the object against different points without it moving around whenever I try to switch between them.
<UserControl x:Class="ObjrctRotation.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:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="C" Background="White">
<Path x:Name="thing" RenderTransformOrigin="0,0" Stretch="Fill" Stroke="Black" StrokeThickness="2" Height="93.153" HorizontalAlignment="Left" VerticalAlignment="Top" Width="106.357" UseLayoutRounding="False" Data="M48.723255,124.82815 C41.00029,80.999809 81.000046,65.000069 112.99985,89.000092 C144.99965,113.00011 168.9996,169.00061 136.99989,169.0002 C105.00018,168.99979 104.9999,145.00015 81.000053,145.00015 C57.000206,145.00015 56.44622,168.65649 48.723255,124.82815 z" Canvas.Left="69" Canvas.Top="24" />
<Slider Value="0" Minimum="0" Maximum="360" Width="100" Margin="300,0,0,272" ValueChanged="Slider_ValueChanged" />
<TextBlock x:Name="test" Width="100" Height="20" Canvas.Left="300" Canvas.Top="24" />
<Button Canvas.Left="300" Canvas.Top="24" Content="ChangeCenter" Height="20" Name="button1" Width="100" Click="button1_Click"/>
</Canvas>
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
RotateTransform rotate = new RotateTransform()
{
Angle=e.NewValue
};
thing.RenderTransform = rotate;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
thing.RenderTransformOrigin = new Point(thing.RenderTransformOrigin.X + 0.2, thing.RenderTransformOrigin.Y + 0.2);
}
}
Set the RenderTransformOrigin="0,0" to RenderTransformOrigin="0.5,0.5" so that the transform origin is in the middle of the object you wish to rotate.

Resources