0

A WPF beginner here transitioning to C# from Qt. I have a custom control with separate XAML and CS files, and this control is later used in MainWindow to interact with users.

The control is a knob / gauge (QDial equivalent). I would like the control to raise an event each time the knob is rotated - enabling the main window to handle the remaining logic. Ideally the event should be raised with an int argument (passing the current value of the knob to the slot function), but for the sake of simplicity lets assume no arguments.

I was inspired by these topics: https://learn.microsoft.com/en-us/answers/questions/35083/events-in-custom-control-in-wpf.html and WPF Custom Controls Construction,Triggers and Events - however what I wrote still doesn't work.

The relevant code is as follows:

Control CS:

  public partial class KnobControl
  {
    public static readonly RoutedEvent valueWasChangedEvent = EventManager.RegisterRoutedEvent("valueWasChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(KnobControl));
    public event RoutedEventHandler valueWasChanged
        {
            add { AddHandler(valueWasChangedEvent, value); }
            remove { RemoveHandler(valueWasChangedEvent, value); }
        }
    
    void onMouseWheelUp() {
      (...)
      RoutedEventArgs args = new RoutedEventArgs(valueWasChangedEvent, this);
      this.RaiseEvent(args);
      }
}

In the MainWindow XAML the control is described as follows:

<local:KnobControl x:Name="KnobBrightness" valueWasChanged="onBrightnessValueChanged" />

And in the MainWindow CS I have - what I believe - is a function which should handle the event:

private void onBrightnessValueChanged(object sender, EventArgs e) 
{
    Debug.WriteLine("Knob value change event has been registered.");
}

However, the above solution will not compile, with VS throwing the following error:

System.Windows.Markup.XamlParseException: ''Failed to create a 'valueWasChanged' from the text 'onBrightnessValueChanged'.'

Would anyone be able to advise what am I doing wrong?

Bart M
  • 697
  • 1
  • 6
  • 18
  • _"I would like the control to raise an event each time the knob is rotated"_ -- the right way to do this is for the control to expose a dependency property that represents the position/value for the "knob" element, so that your correctly-written C# code that uses data binding and proper MVVM practices sees the bound property updated when the dependency property's value changes. See duplicate. – Peter Duniho Jun 28 '21 at 16:57
  • 1
    Derive your control from RangeBase, the base class of Slider and ScrollBar. You would get a bindable Value property and a ValueChanged event for free. – Clemens Jun 28 '21 at 20:10
  • @Clemens 100 x Thank you, this is a very elegant and simple solution, probably the best approach to solve the problem. – Bart M Jun 30 '21 at 19:08
  • @BartM Maybe you do not even need to derive from RangeBase, but just style a Slider control. – Clemens Jun 30 '21 at 19:17

0 Answers0