开发者

Dependency property: how to design

I have 2 custom controls that are independent of each other in a wpf application. However, I have some behavior that needs to repli开发者_运维问答cated in both the controls that is similar.

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 controls.

Is this possible and if so how would one go about designing it?

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?


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.


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜