Click here to Skip to main content
15,881,882 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Weird behavior with contextual menu not rendering properly. Pin
Maximilien22-Sep-22 8:22
Maximilien22-Sep-22 8:22 
QuestionConversion to .Net Core Issues Pin
Kevin Marois19-Sep-22 6:39
professionalKevin Marois19-Sep-22 6:39 
AnswerRe: Conversion to .Net Core Issues Pin
Richard Deeming19-Sep-22 21:20
mveRichard Deeming19-Sep-22 21:20 
GeneralRe: Conversion to .Net Core Issues Pin
Kevin Marois20-Sep-22 7:10
professionalKevin Marois20-Sep-22 7:10 
GeneralRe: Conversion to .Net Core Issues Pin
Kevin Marois22-Sep-22 5:57
professionalKevin Marois22-Sep-22 5:57 
GeneralRe: Conversion to .Net Core Issues Pin
Richard Deeming22-Sep-22 21:38
mveRichard Deeming22-Sep-22 21:38 
QuestionUsing Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves1-Sep-22 10:52
Marc Jeeves1-Sep-22 10:52 
AnswerRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard Deeming1-Sep-22 22:11
mveRichard Deeming1-Sep-22 22:11 
Marc Jeeves wrote:
public partial class WaitTicker_Control : UserControl, INotifyPropertyChanged
A DependencyObject such as a UserControl doesn't need to implement INotifyPropertyChanged. The DependencyProperty already raises the necessary events when the value changes.

You're passing in the current value of a DependencyProperty to the Beating class. At that point, there is no connection between the int in the Beating class and the DependencyProperty.

The simplest option is probably to reverse the connection, and use binding to move the values around when they change:
C#
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;
        }
    }
}
C#
public partial class WaitTicker_Control : UserControl
{
    private Beating _timer1;
    private Beating _timer2;
    private Beating _timer3;
    
    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_Control()
    {
        InitializeComponent();
        _timer1 = CreateBeating("Pulse1", PulseCurrentValue1Property, Pulse1);
        _timer2 = CreateBeating("Pulse2", PulseCurrentValue2Property, Pulse2);
        _timer3 = CreateBeating("Pulse3", PulseCurrentValue3Property, Pulse3);
    }




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves2-Sep-22 4:11
Marc Jeeves2-Sep-22 4:11 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard MacCutchan3-Sep-22 21:01
mveRichard MacCutchan3-Sep-22 21:01 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
mjeeves3-Sep-22 8:58
mjeeves3-Sep-22 8:58 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz4-Sep-22 8:57
mveGerry Schmitz4-Sep-22 8:57 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Richard Deeming5-Sep-22 21:11
mveRichard Deeming5-Sep-22 21:11 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz6-Sep-22 5:34
mveGerry Schmitz6-Sep-22 5:34 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Marc Jeeves6-Sep-22 10:05
Marc Jeeves6-Sep-22 10:05 
GeneralRe: Using Dependency Property's to Affect Non WPF Property's Pin
Gerry Schmitz6-Sep-22 13:19
mveGerry Schmitz6-Sep-22 13:19 
QuestionCan Not Bin to Dependency Property in Custom Control Pin
Marc Jeeves1-Sep-22 6:26
Marc Jeeves1-Sep-22 6:26 
AnswerRe: Can Not Bin to Dependency Property in Custom Control Pin
Richard Deeming1-Sep-22 6:35
mveRichard Deeming1-Sep-22 6:35 
GeneralRe: Can Not Bin to Dependency Property in Custom Control Pin
Marc Jeeves1-Sep-22 6:47
Marc Jeeves1-Sep-22 6:47 
QuestionWhat is the most common MVVM framework used today? Pin
Code4Ever21-Jul-22 7:26
Code4Ever21-Jul-22 7:26 
AnswerRe: What is the most common MVVM framework used today? Pin
Pete O'Hanlon21-Jul-22 21:21
mvePete O'Hanlon21-Jul-22 21:21 
AnswerRe: What is the most common MVVM framework used today? Pin
Richard Deeming21-Jul-22 21:22
mveRichard Deeming21-Jul-22 21:22 
AnswerRe: What is the most common MVVM framework used today? Pin
Gerry Schmitz22-Jul-22 5:10
mveGerry Schmitz22-Jul-22 5:10 
GeneralRe: What is the most common MVVM framework used today? Pin
Pete O'Hanlon24-Jul-22 11:32
mvePete O'Hanlon24-Jul-22 11:32 
GeneralRe: What is the most common MVVM framework used today? Pin
Gerry Schmitz25-Jul-22 20:43
mveGerry Schmitz25-Jul-22 20:43 

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.