C#/WPF - RoutedEvent in WPF class that isn't a UIElement - wpf

I have a class that needs to notify that something significant has occurred. The class is in a WPF-project, even though this specific class, is lookless (and doesn't inherit from UIElement, neither directly or indirectly).
Normally, I just register a RoutedEvent to get this functionality but as this class neither has AddHandler nor RemoveHandler, I can't get it to work. Anyone knows of another way of get the RoutedEvent behaviour?

As far as I know, if your class isn't a UIElement, it cannot be part of the visual tree, and if it isn't part of the visual tree, you cannot throw RoutedEvents. They're strictly a UI concept.
I think the recommended approach would be to either make your class inherit from UIElement, or if that's not possible/desired, create a counterpart for your class which does inherit from UIElement and use this second class in the visual tree where you would normally place your original class.

Related

How to apply FrameworkPropertyMetadata.AffectsArrange to a DependencyProperty

In a custom control I have a class which derives from DependencyObject and has a dependency property called MaxIdealWidth. I have several custom controls which do some pretty weird measure/arrange, and they all use this property in some way, but not in a binding.
My problem is, when everything is drawn the first time, it works well. However when the MaxIdealWidth is changed by one custom control, none of the others will do measure/arrange. I can understand why this happens, but I need to force all custom controls to measure/arrange at the same time.
FrameworkPropertyMetadata.AffectsArrange
Looking at the documentation, this looks like a promising way forward, however I have absolutely no idea how to apply it in practice. It doesn't seem to be documented from a point of view of how to use it in my situation. Can someone tell me how to apply AffectsArrange from XAML or code-behind to indicate from a custom control that a dependency property on the DataContext should cause measure/arrange?
You do that by using the FrameworkPropertyMetadata class instead of the PropertyMetadata for the last Parameter of the dependency propery Registration. There you set all the bits you like:
public static readonly DependencyProperty ArrowEndsProperty =
DependencyProperty.Register("your name",
typeof(<class>), typeof(<owner class>),
new FrameworkPropertyMetadata(<initial value>,
FrameworkPropertyMetadataOptions.AffectsMeasure));

Changing exisiting standard property to DependencyProperty

Is that possible to change for example MediaElement.Position property to DependencyProperty by inheriting MediaElement class and then creating DependencyProperty in a new class from inherited class?
How to do it? AFAIK DependencyProperty stops normal behaviour of default accessor, how to reconnect things to not break up after change?
I want to update Slider.Value through Binding thus I need MediaElement.Position as DependencyProperty. I know I can do it with DispatcherTimer but I think it's not professional solution.
You can create either a custom control (a XAML-less control that inherits from MediaElement) or a UserControl with a MediaElement somewhere in its visual tree.
In either case, create your DependencyProperty, and use the PropertyChangedCallBack (in FrameworkPropertyMetadata) to respond to changes made, and use events to listen the other way.
In either case, you are creating a new control to use instead of the MediaElement. The advantage of a User Control is that you only expose the properties needed. The disadvantage being naturally that you have to expose them all. The custom control will also expose the Position property even though you don't want to use it.

WPF+MVVM: How to use plain old ViewModelBase when DependencyProperty is needed

I am using a 3rd party WPF control whose MVVM support relies on dependency properties on the VM it is bound to. The sample that comes with the control uses a ViewModelBase class derived from DependencyObject so all is well.
My ViewModelBase implements INotifyPropertyChanged and for various reasons it is unrealistic to change it to DependencyObject.
My question is how do I use my ViewModels with this WPF control? I guess what I need is something like "embedding a dependencyobject" or "plugging dependency properties" in a plain old ViewModel.
By the way my MVVM application is interface based, i.e. everywhere SomeViewModel is ISomeViewModel.
In general, a properly designed control shouldn't require binding to a DependencyProperty, as a DP can bind to any property without issue. As such, I'd revisit whether this is truly a bug in the control implementation first, and correct that.
However, if you must do this, realize you're going to violate MVVM - using DependencyObject within a ViewModel is, by its very nature, injecting view specific framework elements into the VM. Once you decide you're okay with doing this, you can always have your ViewModel expose a DependencyObject as a property, and bind to a DependencyProperty defined on that DependencyObject instead of directly to your VM's property.

What is the WPF equivalent of Silverlight's ScrollViewer.ScrollIntoView?

What is the WPF equivalent of Silverlight's ScrollViewer.ScrollIntoView?
The FrameworkElement class implements a BringIntoView() method - if you are dealing with a class that inherits from FrameworkElement you should be able to call that method. The method essentially raises the RequestBringIntoViewEvent which will bubble up the visual tree. The ScrollViewer and a bunch of other classes handle the event and then call their internal logic to bring the element into view.
Also some ItemControls such as DataGrid or ListBox provide a ScrollIntoView() method to make a child visible.
The ScrollIntoView() in turns calls the OnBringItemIntoView method in the ItemsControl class and in turn calls the FrameworkElement but also deals with a VirtualizingPanel where you might not have a FrameworkElement already created.

Why has the selector class an internal Constructor?

I tried to derive from the Selector class cause I need a similar functionality as the ListBox but it is no ListBox.
I had a look at the signature of the Selector class and it is (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector(v=vs.95).aspx)
public abstract class Selector : ItemsControl,
ISupportInitialize
But the problem is that the constructor is internal. So it is not possible to derive from this class outside the assembly (ListBox and ComboBox are in this assembly).
I now derived from the ListBox to achieve my goal, but my question is:
Why has the selector class an internal Constructor?
Because the Selector class is abstract. You can't create instances of abstract classes, and the easiest way to make sure you can't even do that by mistake (in a regular way) is to not make a constructor available.
I don't see an entry for the constructor on the MSDN, but my bet is that it's probably a protected constructor, not an internal one.
But from what I can see, nothing stops you from deriving from Selector, and create your custom implementation.
Edit:
Reflector shows the constructor to be internal indeed, so no deriving...

Resources