Click here to Skip to main content
15,507,425 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Conversion to .Net Core Issues Pin
Kevin Marois20-Sep-22 8:10
professionalKevin Marois20-Sep-22 8:10 
GeneralRe: Conversion to .Net Core Issues Pin
Kevin Marois22-Sep-22 6:57
professionalKevin Marois22-Sep-22 6:57 
GeneralRe: Conversion to .Net Core Issues Pin
Richard Deeming22-Sep-22 22:38
mveRichard Deeming22-Sep-22 22:38 
QuestionUsing Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves1-Sep-22 11:52
Marc Jeeves1-Sep-22 11:52 
AnswerRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard Deeming1-Sep-22 23:11
mveRichard Deeming1-Sep-22 23:11 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves2-Sep-22 5:11
Marc Jeeves2-Sep-22 5:11 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard MacCutchan3-Sep-22 22:01
mveRichard MacCutchan3-Sep-22 22:01 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
mjeeves3-Sep-22 9:58
mjeeves3-Sep-22 9:58 
So I have three last issues, thanks for all the help

1) if I use the usercontrol name "Name="WaitTickerCustomControl" then the Custom Control disappears in the Main Window odd.

2) The directly Bound dependency property are bound but nothing updates

3) The Indirectly Bound property's only affect the third ticker odd...

Thanks for the help im learning alot.

Madaxe

XAML Custom Control
<pre><UserControl x:Class="Testing.CustomControls.WaitTicker_CustomControl"
             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:local="clr-namespace:Testing.CustomControls"
             mc:Ignorable="d" 
             d:DesignHeight="70" d:DesignWidth="60"
             Name="WaitTickerCustomControl">
    <Grid Name="WaitTickerMainGrid" Visibility="{Binding Path=ControlHideShow,ElementName=WaitTickerCustomControl, FallbackValue=Visible}">
        <Border Name="Pulse1" 
                CornerRadius="{Binding Path=ControlCornerRadius1,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Height="{Binding Path=PulseHeight1,ElementName=WaitTickerCustomControl,FallbackValue=50}"
                Width="{Binding Path=PulseWidth1,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Margin="10,10,0,0"
                Background="{Binding Path=ControlBackground1,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}" 
                HorizontalAlignment="Left" VerticalAlignment="Top"/>

        <Border Name="Pulse2"
                CornerRadius="{Binding Path=ControlCornerRadius2,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Height="{Binding Path=PulseHeight2,ElementName=WaitTickerCustomControl,FallbackValue=50}"
                Width="{Binding Path=PulseWidth2,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Margin="25,10,0,0"
                Background="{Binding Path=ControlBackground2,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}" 
                HorizontalAlignment="Left" VerticalAlignment="Top"/>

        <Border Name="Pulse3"
                CornerRadius="{Binding Path=ControlCornerRadius3,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Height="{Binding Path=PulseHeight3,ElementName=WaitTickerCustomControl,FallbackValue=50}"
                Width="{Binding Path=PulseWidth3,ElementName=WaitTickerCustomControl,FallbackValue=10}"
                Margin="40,10,0,0"
                Background="{Binding Path=ControlBackground3,ElementName=WaitTickerCustomControl,FallbackValue=#4287f5}" 
                HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
</UserControl>



XAML Code Behind

public partial class WaitTicker_CustomControl : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        //Directly Bound to XAML
        public Visibility ControlHideShow
        {
            get { return (Visibility)GetValue(ControlHideShowProperty); }
            set { SetValue(ControlHideShowProperty, value); }
        }
        public static readonly DependencyProperty ControlHideShowProperty =
            DependencyProperty.Register("ControlHideShow", typeof(Visibility), 
                typeof(WaitTicker_CustomControl),
               new PropertyMetadata(Visibility.Hidden));
        public int ControlCornerRadius1
        {
            get { return (int)GetValue(ControlCornerRadius1Property); }
            set { SetValue(ControlCornerRadius1Property, value); }
        }
        public static readonly DependencyProperty ControlCornerRadius1Property =
            DependencyProperty.Register("ControlCornerRadius1", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(10));
        public int ControlCornerRadius2
        {
            get { return (int)GetValue(ControlCornerRadius2Property); }
            set { SetValue(ControlCornerRadius2Property, value); }
        }
        public static readonly DependencyProperty ControlCornerRadius2Property =
            DependencyProperty.Register("ControlCornerRadius2", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(10));
        public int ControlCornerRadius3
        {
            get { return (int)GetValue(ControlCornerRadius3Property); }
            set { SetValue(ControlCornerRadius3Property, value); }
        }
        public static readonly DependencyProperty ControlCornerRadius3Property =
            DependencyProperty.Register("ControlCornerRadius3", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(10));
        public int PulseHeight1
        {
            get { return (int)GetValue(PulseHeight1Property); }
            set { SetValue(PulseHeight1Property, value); }
        }
        public static readonly DependencyProperty PulseHeight1Property =
            DependencyProperty.Register("PulseHeight1", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(1));
        public int PulseHeight2
        {
            get { return (int)GetValue(PulseHeight2Property); }
            set { SetValue(PulseHeight2Property, value); }
        }
        public static readonly DependencyProperty PulseHeight2Property =
            DependencyProperty.Register("PulseHeight2", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(25));
        public int PulseHeight3
        {
            get { return (int)GetValue(PulseHeight3Property); }
            set { SetValue(PulseHeight3Property, value); }
        }
        public static readonly DependencyProperty PulseHeight3Property =
            DependencyProperty.Register("PulseHeight3", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(50));
        public int PulseWidth1
        {
            get { return (int)GetValue(PulseWidth1Property); }
            set { SetValue(PulseWidth1Property, value); }
        }
        public static readonly DependencyProperty PulseWidth1Property =
            DependencyProperty.Register("PulseWidth1", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(20));
        public int PulseWidth2
        {
            get { return (int)GetValue(PulseWidth2Property); }
            set { SetValue(PulseWidth2Property, value); }
        }
        public static readonly DependencyProperty PulseWidth2Property =
            DependencyProperty.Register("PulseWidth2", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(20));
        public int PulseWidth3
        {
            get { return (int)GetValue(PulseWidth3Property); }
            set { SetValue(PulseWidth3Property, value); }
        }
        public static readonly DependencyProperty PulseWidth3Property =
            DependencyProperty.Register("PulseWidth3", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(0));

        public Brush ControlBackground1
        {
            get { return (Brush)GetValue(ControlBackground1Property); }
            set { SetValue(ControlBackground1Property, value); }
        }
        public static readonly DependencyProperty ControlBackground1Property =
            DependencyProperty.Register("ControlBackground1", typeof(Brush),
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(Brushes.Blue));
        public Brush ControlBackground2
        {
            get { return (Brush)GetValue(ControlBackground2Property); }
            set { SetValue(ControlBackground2Property, value); }
        }
        public static readonly DependencyProperty ControlBackground2Property =
            DependencyProperty.Register("ControlBackground2", typeof(Brush),
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(Brushes.Blue));
        public Brush ControlBackground3
        {
            get { return (Brush)GetValue(ControlBackground3Property); }
            set { SetValue(ControlBackground3Property, value); }
        }
        public static readonly DependencyProperty ControlBackground3Property =
            DependencyProperty.Register("ControlBackground3", typeof(Brush),
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(Brushes.Blue));


        private Beating _timer1;
        private Beating _timer2;
        private Beating _timer3;

        // Indirectly Bound to XAML
        public int PulseIncrementValue
        {
            get { return (int)GetValue(PulseIncrementValueProperty); }
            set { SetValue(PulseIncrementValueProperty, value); }
        }
        public static readonly DependencyProperty PulseIncrementValueProperty =
            DependencyProperty.Register("PulseIncrementValue", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(1));
        public int PulseMaxValue
        {
            get { return (int)GetValue(PulseMaxValueProperty); }
            set { SetValue(PulseMaxValueProperty, value); }
        }
        public static readonly DependencyProperty PulseMaxValueProperty =
            DependencyProperty.Register("PulseMaxValue", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(50));
        public int PulseMinValue
        {
            get { return (int)GetValue(PulseMinValueProperty); }
            set { SetValue(PulseMinValueProperty, value); }
        }
        public static readonly DependencyProperty PulseMinValueProperty =
            DependencyProperty.Register("PulseMinValue", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(10));
        public double PulseSpeedValue
        {
            get { return (double)GetValue(PulseSpeedValueProperty); }
            set { SetValue(PulseSpeedValueProperty, value); }
        }
        public static readonly DependencyProperty PulseSpeedValueProperty =
            DependencyProperty.Register("PulseSpeedValue", typeof(double), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(0.01));
        public int PulseCurrentValue1
        {
            get { return (int)GetValue(PulseCurrentValue1Property); }
            set { SetValue(PulseCurrentValue1Property, value); }
        }
        public static readonly DependencyProperty PulseCurrentValue1Property =
            DependencyProperty.Register("PulseCurrentValue1", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(5));
        public int PulseCurrentValue2
        {
            get { return (int)GetValue(PulseCurrentValue2Property); }
            set { SetValue(PulseCurrentValue2Property, value); }
        }
        public static readonly DependencyProperty PulseCurrentValue2Property =
            DependencyProperty.Register("PulseCurrentValue2", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(20));
        public int PulseCurrentValue3
        {
            get { return (int)GetValue(PulseCurrentValue3Property); }
            set { SetValue(PulseCurrentValue3Property, value); }
        }
        public static readonly DependencyProperty PulseCurrentValue3Property =
            DependencyProperty.Register("PulseCurrentValue3", typeof(int), 
                typeof(WaitTicker_CustomControl),
                new PropertyMetadata(40));

        private Beating CreateBeating(string beatName, DependencyProperty valueProperty, Border pulse)
        {
            Beating result = new Beating((int)GetValue(valueProperty), PulseIncrementValue, PulseMinValue, PulseMaxValue, beatName, PulseSpeedValue);

            // Push changes to the control's properties down to the Beating instance:
            SetBinding(PulseIncrementValueProperty, new Binding(nameof(Beating.Increment)) { Source = result, Mode = BindingMode.OneWayToSource });
            SetBinding(PulseMinValueProperty, new Binding(nameof(Beating.MinValue)) { Source = result, Mode = BindingMode.OneWayToSource });
            SetBinding(PulseMaxValueProperty, new Binding(nameof(Beating.MaxValue)) { Source = result, Mode = BindingMode.OneWayToSource });
            SetBinding(PulseSpeedValueProperty, new Binding(nameof(Beating.Speed)) { Source = result, Mode = BindingMode.OneWayToSource });

            // Bind the border's height to the Beating's value:
            pulse.SetBinding(FrameworkElement.HeightProperty, new Binding(nameof(Beating.Value)) { Source = result, Mode = BindingMode.OneWay });

            return result;
        }
        public WaitTicker_CustomControl()
        {
            InitializeComponent();
            _timer1 = CreateBeating("Pulse1", PulseCurrentValue1Property, Pulse1);
            _timer2 = CreateBeating("Pulse2", PulseCurrentValue2Property, Pulse2);
            _timer3 = CreateBeating("Pulse3", PulseCurrentValue3Property, Pulse3);
        }
    }
    internal sealed class Beating : INotifyPropertyChanged
    {
        private readonly Timer? _timer;
        private int _value;
        private bool _direction;

        public int Value
        {
            get { return _value; }
            set
            {
                if (value != _value)
                {
                    _value = value;
                    OnPropertyChanged();
                }
            }
        }
        public int Increment { get; set; }
        public int MinValue { get; set; }
        public int MaxValue { get; set; }
        public string BeatName { get; }

        public double Speed
        {
            get { return _timer.Interval; }
            set { _timer.Interval = value; }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public Beating(int initialValue, int increment, int minValue, int maxValue, string beatName, double speed)
        {
            _value = initialValue;
            Increment = increment;
            MinValue = minValue;
            MaxValue = maxValue;
            BeatName = beatName;

            _timer = new Timer
            {
                Interval = speed,
                AutoReset = true,
                Enabled = true,
            };

            _timer.Elapsed += OnTimedEvent;
        }
        public void StartTimer()
        {
            _timer.Start();
        }
        public void StopTimer()
        {
            _timer.Stop();
        }
        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            if (!_direction)
            {
                Value -= Increment;
                if (Value < MinValue)
                    _direction = true;
            }
            else
            {
                Value += Increment;
                if (Value > MaxValue)
                    _direction = false;
            }
        }
    }


Custom Control Implementation
<Window x:Class="Testing.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:Testing" xmlns:customcontrols="clr-namespace:Testing.CustomControls"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <customcontrols:WaitTicker_CustomControl
            Height="100" Width="100"
            VerticalAlignment="Top" HorizontalAlignment="Left"
            Margin="10 10 0 0"
            PulseMaxValue="100"
            PulseMinValue="5"
            PulseHeight1="100"
            PulseHeight2="100"
            PulseHeight3="100"
            PulseSpeedValue="50"
            ControlCornerRadius1="5"
            ControlCornerRadius2="5"
            ControlCornerRadius3="5"
            ControlBackground1="Aquamarine"
            ControlBackground2="Yellow"
            ControlBackground3="Orange"/>
    </Grid>
</Window>


modified 3-Sep-22 18:15pm.

GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz4-Sep-22 9:57
mveGerry Schmitz4-Sep-22 9:57 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard Deeming5-Sep-22 22:11
mveRichard Deeming5-Sep-22 22:11 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz6-Sep-22 6:34
mveGerry Schmitz6-Sep-22 6:34 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves6-Sep-22 11:05
Marc Jeeves6-Sep-22 11:05 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz6-Sep-22 14:19
mveGerry Schmitz6-Sep-22 14:19 
QuestionCan Not Bin to Dependency Property in Custom Control Pin
Marc Jeeves1-Sep-22 7:26
Marc Jeeves1-Sep-22 7:26 
AnswerRe: Can Not Bin to Dependency Property in Custom Control Pin
Richard Deeming1-Sep-22 7:35
mveRichard Deeming1-Sep-22 7:35 
GeneralRe: Can Not Bin to Dependency Property in Custom Control Pin
Marc Jeeves1-Sep-22 7:47
Marc Jeeves1-Sep-22 7:47 
QuestionWhat is the most common MVVM framework used today? Pin
Code4Ever21-Jul-22 8:26
Code4Ever21-Jul-22 8:26 
AnswerRe: What is the most common MVVM framework used today? Pin
Pete O'Hanlon21-Jul-22 22:21
mvaPete O'Hanlon21-Jul-22 22:21 
AnswerRe: What is the most common MVVM framework used today? Pin
Richard Deeming21-Jul-22 22:22
mveRichard Deeming21-Jul-22 22:22 
AnswerRe: What is the most common MVVM framework used today? Pin
Gerry Schmitz22-Jul-22 6:10
mveGerry Schmitz22-Jul-22 6:10 
GeneralRe: What is the most common MVVM framework used today? Pin
Pete O'Hanlon24-Jul-22 12:32
mvaPete O'Hanlon24-Jul-22 12:32 
GeneralRe: What is the most common MVVM framework used today? Pin
Gerry Schmitz25-Jul-22 21:43
mveGerry Schmitz25-Jul-22 21:43 
GeneralRe: What is the most common MVVM framework used today? Pin
Pete O'Hanlon25-Jul-22 22:30
mvaPete O'Hanlon25-Jul-22 22:30 
QuestionIs it possible to do event handling in the ViewModel? Pin
Code4Ever16-Jul-22 7:39
Code4Ever16-Jul-22 7:39 
AnswerRe: Is it possible to do event handling in the ViewModel? Pin
Gerry Schmitz17-Jul-22 7:06
mveGerry Schmitz17-Jul-22 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.