views:

33

answers:

3

Hi, I have 2 custom controls that are independent of each other in a wpf application. However, I have some behavior that needs to replicated in both the controls that is similiar.

For example, when a user does drag drop on either of these 2 controls, i need more or less the same behavior to execute.

The same behavior also consists of a dependency property that needs to be shared across the 2 cotrols.

My Q is, is this possible and if so how would one go about designing it? Or am i totally off track here....Apologies if the Q seems to be silly...Im new to wpf.

So basically, how do I share a dependency property across controls, and also share some behavior as well, when the controls are independent of each other?

Thanks in advance!

A: 

Well, you could inherit those 2 controls from a your custom base control that has the dragging logic that you need in both of them. Just like ListBox and ComboBox derive from ItemsControl. That way, they share the same properties and behavior.

If these two controls cannot inherit from the same control, you could implement a common interface just like ICommandSource that is implemented by a lot of controls.

decyclone
A: 

Use inheritance - create a common base control containing the logic you need to be the same in both controls and then in their XAML use something like this:

<local:BaseClassName
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:YourNamespaceName"  
mc:Ignorable="d"
x:Class="ChildControlClassName"
x:Name="ChildControlName"
d:DesignWidth="640" 
d:DesignHeight="480" 
>
    ...control XAML
</local:BaseClassName>
CommanderZ
A: 

Or use Attached DependencyProperties. Declare your properties on a third class - here is an example of how you could handle DoubleClicking (should be much the same logic) - makes it much easier to re-use without tightly coupling the two controls - especially if one is a Panel and the other an ItemsControl: http://www.codeproject.com/Articles/42111/Selector-DoubleClick-Behaviour-calling-ViewModel-I.aspx

Goblin