Change image after button click using MahApps Metro - wpf

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"));
}

Related

Change image of a templated button in wpf

I have a templated wpf button. I have to change the image at runtime.
xaml code:
<Window.Resources>
<Image x:Key="imgPlay" Source="Media/Knob Play.png"></Image>
<Image x:Key="imgStop" Source="Media/Knob Red.png"></Image>
<ControlTemplate x:Key="custom-button" TargetType="Button">
<Grid x:Name="btn_image">
<!--<Grid.Background>
<ImageBrush ImageSource="Media/Knob Red.png"></ImageBrush>
</Grid.Background>-->
<!--<Image Source="Media/Knob Red.png"></Image>-->
</Grid>
</ControlTemplate>
</Window.Resources>
button i need to change:
<Button Name="start" Template="{DynamicResource custom-button}" HorizontalAlignment="Left" Margin="147,67,0,0" VerticalAlignment="Top" Width="37" Height="30" Click="Start_Click">
<DynamicResource ResourceKey="imgStop"></DynamicResource>
</Button>
codebehind:
private void Start_Click(object sender, RoutedEventArgs e)
{
if (sw.IsRunning)
{
start.Content = FindResource("imgStop");
sw.Stop();
dt.Stop();
}
else
{
sw.Start();
dt.Start();
start.Content = FindResource("imgPlay");
}
}
tried many solutions in SO and net .Nothing worked.
Maybe you can get an inspiration from this...
Resource
<Window.Resources>
<ControlTemplate x:Key="option1" TargetType="Button">
<TextBlock Foreground="Red">ClickMe</TextBlock>
</ControlTemplate>
<ControlTemplate x:Key="option2" TargetType="Button">
<TextBlock Foreground="Green">Template is changed</TextBlock>
</ControlTemplate>
</Window.Resources>
XAML:
<Grid Width="100" Height="100">
<Button Name="theButton" Click="OnClick" Template="{StaticResource option1}"/>
</Grid>
Code behind:
private void OnClick(object sender, RoutedEventArgs e)
{
this.theButton.Template = (ControlTemplate)FindResource("option2");
}

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

How to remove any formatting(bold, italic, fonts, font size) from a richtextbox WPF

I am making a text editor program in WPF and I need to make a Plain button, which it's purpose is to remove the formatting (bold, italic, font such as Arial, font size) of text of the RichTextBox.
This is my code so far:
<Window x:Class="TextEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TextEditor"
Title="Text Editor" Height="480" Width="640">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File" Name="Menu">
<MenuItem Header="New" Name="New"
Click="New_Click" />
<MenuItem Header="Save" Name="Save"
Click="Save_Click" />
</MenuItem>
</Menu>
<DockPanel DockPanel.Dock="Top">
<ToolBar>
<ToggleButton x:Name="boldButton"
ToolTip="Bold"
Command="{x:Static EditingCommands.ToggleBold}" CommandTarget="{Binding ElementName=_richTextBox}">
<Image Source="Icons/text_bold.png"
Height="25"
Width="25"/>
</ToggleButton>
<ToggleButton x:Name="italicButton"
ToolTip="Italic"
Command="{x:Static EditingCommands.ToggleItalic}" CommandTarget="{Binding ElementName=_richTextBox}">
<Image Source="Icons/text_italic.png"
Height="25"
Width="25"/>
</ToggleButton>
<ToggleButton x:Name="PlainButton"
ToolTip="Make the text plain"
Click="PlainButton_Click">
PlainText
</ToggleButton>
<Button x:Name="BackgroundButton"
ToolTip="Change the background of the textbox"
Click="BackgroundButton_Click">
Change the background
</Button>
<Separator/>
<ComboBox x:Name="fonts"
MinWidth="100"
DataContext="{x:Static Fonts.SystemFontFamilies}"
ItemsSource="{Binding}"
ToolTip="Font"
SelectionChanged="Font_SelectionChanged"/>
<ComboBox x:Name="fontSize"
MinWidth="40"
ToolTip="Font Size"
>
</ComboBox>
</ToolBar>
</DockPanel>
<StatusBar DockPanel.Dock="Bottom">
<TextBlock x:Name="status"/>
</StatusBar>
<RichTextBox x:Name="body"
FontSize="{Binding ElementName=fontSize, Path=SelectedItem}"
SpellCheck.IsEnabled="True"
AcceptsReturn="True"
AcceptsTab="True"
SelectionChanged="body_SelectionChanged"
BorderThickness="0 2 0 0"/>
</DockPanel>
</Window>
C sharp code:
public MainWindow()
{
InitializeComponent();
for (double i = 8; i <= 48; i += 2)
{
fontSize.Items.Add(i);
}
}
private void body_SelectionChanged(object sender, RoutedEventArgs e)
{
object temp = body.Selection.GetPropertyValue(Inline.FontFamilyProperty);
fonts.SelectedItem = temp;
}
private void New_Click(object sender, RoutedEventArgs e)
{
body.Document.Blocks.Clear();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text file|*.txt";
sfd.FileName = "Untitled document";
if (sfd.ShowDialog() == true)
{
FileStream fileStream = new FileStream(sfd.FileName, FileMode.Create);
TextRange range = new TextRange(body.Document.ContentStart, body.Document.ContentEnd);
range.Save(fileStream, DataFormats.Text);
}
}
private void Font_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (fonts.SelectedItem != null)
body.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, fonts.SelectedItem);
}
private void PlainButton_Click(object sender, RoutedEventArgs e)
{
//the code of the plain button
}
private void BackgroundButton_Click(object sender, RoutedEventArgs e)
{
body.Background = Brushes.Yellow;
}
It's a solution from 2009 ... I don't know if the richtext API in .NET has improved, but this post seems to have a solution for what you need.
Use the following code for WPF, if you are still looking for answers.
myRichTextBox.SelectAll();
myRichTextBox.Selection.ClearAllProperties();

move canvas on key down

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;
}
}

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" />

Resources