C# - WPF - How to create a custom and animated RepeatButton at backcode?

43 Views Asked by At

I am trying to create RepeatButton at xaml.cs code with an animated icon in place of a button. For that, I had written a xaml code and C# backcode for a window. Here is the XAML code (.xaml):

<Window x:Class="MVPInterface.TheTestWindow"
        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:MVPInterface"
        mc:Ignorable="d"
        Title="The Screen" Height="1080" Width="1920" AllowsTransparency="True" WindowStyle="None" Background="{x:Null}" ResizeMode="CanMinimize" WindowState="Maximized">

    <Border Name="theWindow" CornerRadius="10" Background="Azure" BorderBrush="Gray" BorderThickness="3" ScrollViewer.VerticalScrollBarVisibility="Disabled" Focusable="True">
        
        <Grid>
            
            <StackPanel x:Name="ButtonHolder"  Grid.Row="2" Margin="1674,559,40,20">
    
    
    
            </StackPanel>
            
        </Grid>

    </Border>

</Window>

And here is the C# backcode I wrote (.xaml.cs):


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.Shapes;

namespace MVPInterface
{
    /// <summary>
    /// Interaction Logic
    /// </summary>
    public partial class TheTestWindow : Window
    {
        public TheTestWindow()
        {
            InitializeComponent();

            UpdateTree();

        }

        public void UpdateTree()
        {

            RepeatButton increase = new RepeatButton();

            Style buttonStyle = new Style(typeof(RepeatButton));
            ControlTemplate template = new ControlTemplate(typeof(RepeatButton));

            FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
            gridFactory.Name = "ContentGrid";

            FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image));
            imageFactory.Name = "PointerImage";
            imageFactory.SetValue(Image.WidthProperty, 20.0);
            imageFactory.SetValue(Image.HeightProperty, 20.0);
            imageFactory.SetValue(Image.StretchProperty, Stretch.UniformToFill);
            imageFactory.SetValue(Image.SourceProperty, new BitmapImage(new Uri("/Img/Pointer.png", UriKind.Relative)));

            gridFactory.AppendChild(imageFactory);
            template.VisualTree = gridFactory;

            Trigger mouseOverTrigger = new Trigger { Property = UIElement.IsMouseOverProperty, Value = true };
            mouseOverTrigger.Setters.Add(new Setter { TargetName = "PointerImage", Property = Image.SourceProperty, Value = new BitmapImage(new Uri("/Img/Pointer_White.png", UriKind.Relative)) });

            Trigger pressedTrigger = new Trigger { Property = ButtonBase.IsPressedProperty, Value = true };
            pressedTrigger.Setters.Add(new Setter { TargetName = "PointerImage", Property = Image.SourceProperty, Value = new BitmapImage(new Uri("/Img/Pointer_White.png", UriKind.Relative)) });

            template.Triggers.Add(mouseOverTrigger);
            template.Triggers.Add(pressedTrigger);

            // Set the template
            increase.Template = template;
            increase.Style = buttonStyle;
            ButtonHolder.Children.Add(increase);

        }
    }
}

When I run it, I see stackpanel in live visual tree without the button added and I can't see the button on the window.

What I was expecting?

I was expecting that to the right side of the window I could see gray button displayed and that, when I hover or click it, it would turn white.

0

There are 0 best solutions below