I have a DependencyObject (an Interactivity Behavior), and I'd like to get its x:Name (just get, not set) from code. Is it possible?
EDIT: Following AnthonyWJones's answer:
I've inserted the following code into my base behavior:
[EditorBrowsable(EditorBrowsableState.Never)]
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(BaseBehavior<T>), new PropertyMetadata(null));
I've given my behaviors x:Name, yet the Name property doesn't get filled.
If you want your Name property to be the same as the x:Name XAML name, then instead of implementing your own dependcy property, piggy-back on the existing one that is already registered. You can simply implement your name property as:
public string Name
{
get { return (string) base.GetValue(FrameworkElement.NameProperty); }
set { base.SetValue(FrameworkElement.NameProperty, value); }
}
If the class deriving from DependencyObject does not expose a Name property then you cannot determine the assigned x:Name. The x:Name value is store only in an internal object tree and there is no API to resolve value (the object) back to a key value (the name).
However if this is your own behaviour then simply add a Name dependency property to your behaviour. x:Name will assign is value to a Name property if present.
If this an existing behaviour you may be able to inherit from it to create a new class that has a Name property. Unfortunately some behaviours are sealed so you can't always do this.
You can create 'Name' AttachedProperty, and use NameAttachedProperty.GetName(DependencyObject)
Of course you will have to attach it to your element before using.
public static class NameAttachedProprty
{
public static readonly DependencyProperty NameProperty =
DependencyProperty.RegisterAttached("Name", typeof (string), typeof (NameAttachedProprty), new PropertyMetadata(default(string)));
public static void SetName(DependencyObject element, string value)
{
element.SetValue(NameProperty, value);
}
public static string GetName(DependencyObject element)
{
return (string) element.GetValue(NameProperty);
}
}
Related
I am rewriting a Delphi 7 application in WPF. One requirement is that all labels, headings, messages etc. are translatable to another language. I am required to use the existing in-house translation engine, versus .NET i18n using resources.
I would very much like to extend the Binding markup extension to be able to specify the language the resultant string is in. This will only be for one way binding, e.g. a simple {"Binding NameLabel Lang=Twi"} so the label for the Name field is displayed in Twi.
I'm sure I can inherit from the binding object, and override some method to call the translation service (through Service Locator} just before it delivers the value asked for from the data context.
You can derived from Binding but you can't override the behaviour to change the returned value i.e. you can't intercept the code in between because ProvideValue method is marked as sealed in BindingBase class so you can't override it and change in derived class.
Definition in BindingBase:
public override sealed object ProvideValue(IServiceProvider serviceProvider);
But in case you interested in custom markup extension which will return i18n string, you can do this way:
public class CustomBinding : MarkupExtension
{
public string Lang { get; set; }
public static object GetValue(DependencyObject obj)
{
return obj.GetValue(ValueProperty);
}
public static void SetValue(DependencyObject obj, Binding value)
{
obj.SetValue(ValueProperty, value);
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.RegisterAttached("Value", typeof(object),
typeof(CustomBinding));
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget pvt = serviceProvider as IProvideValueTarget;
DependencyObject targetObject = pvt.TargetObject as DependencyObject;
if (targetObject == null)
{
return String.Empty;
}
string value = GetValue(targetObject).ToString();
// You get the bound value here. Put i18n code here
// and return actual value. (Access Lang property to get the culture)
return value;
}
}
As you can see i have declare attached property instead of normal DP because for that you have to derive from DependencyObject but we are already deriving from MarkupExtension so can't do multiple inheritance. Have to settle with attached property to provide binding support from XAML.
Usage in XAML:
<TextBlock local:CustomBinding.Value="{Binding PropertyName}"
Text="{local:CustomBinding Lang=Twi}"/>
Of course you have to define local namespace in XAML.
I declared a dependency property with FrameworkPropertyMetadataOptions.Inherits:
public static class DesignerItemStyles {
public static readonly DependencyProperty HeaderBackgroundProperty =
DependencyProperty.RegisterAttached(
"HeaderBackground", typeof(Brush), typeof(DesignerItemStyles),
new FrameworkPropertyMetadata(
Brushes.DesignerViewElementHeaderBackground,
FrameworkPropertyMetadataOptions.Inherits));
/* Below are Get & Set as usual */
}
It kind of works, but somehow not throughout the visual tree. Here is a screenshot showing ContentPresenter that inherit value from HeaderedDesignerItemChrome:
And now, a screenshot showing content of the ContentPresenter, and it does not inherit the value. Nor it is set to something else - it is a default value:
Any idea why?
Using this is not that straight forward since there are some rules that need to be followed to implement property with inheritable values. Here they are:
On parent, dependency property must be defined as attached property. You can still declare property getter/setter, but property must be attached. Here is simple declaration:
public static readonly DependencyProperty InheritedValueProperty =
DependencyProperty.RegisterAttached("InheritedValue",
typeof(int), typeof(MyClass), new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
public static int GetInheritedValue(DependencyObject target)
{
return (int)target.GetValue(InheritedValueProperty);
}
public static void SetInheritedValue(DependencyObject target, int value)
{
target.SetValue(InheritedValueProperty, value);
}
public int InheritedValue
{
get
{
return GetTimeSlotDuration(this);
}
set
{
SetTimeSlotDuration(this, value);
}
}
Child objects would define their instance of the property with inherited value using AddOwner. Following is the code that goes into say MyChildClass sample class:
public static readonly DependencyProperty InheritedValueProperty;
public int InheritedValue
{
get
{
return (int)GetValue(InheritedValueProperty);
}
set
{
SetValue(InheritedValueProperty, value);
}
}
static MyChildClass()
{
InheritedValueProperty =
MyClass.InheritedValueProperty.AddOwner(typeof(MyChildClass),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
}
The global default value is preserved and inheritance still works, if the single argument overload is used...
MyClass.InheritedValueProperty.AddOwner(typeof(MyChildClass));
Note that property is in child class declared as standard dependency property and that it specifies Inherit in meta-data options.
With setup like this now when MyChildClass in parented to MyClass visually or logically they will share the same property value automatically.
So technically, what you see in the Visual Tree is doing what you told it to do. It set the default value that you told it to and the inherited controls inherit from the value of the parent which is your ContentPresenter
Eliminating one of the two ContentPresenters (visible on both screenshots just above DesignerItemsPresenter) worked for me. I am inclined to believe that was a bug in WPF framework itself.
How can I change the default value for an inherited dependency property? In our case, we've created a subclass of Control which by default has its Focusable set to 'true'. We want our subclass to have the default of 'false'.
What we've been doing is simply setting it to 'false' in the constructor, but if someone uses ClearValue, it goes back to the default, not the value set in the constructor.
Here's what I'm currently doing to achieve this (This is a test control with a DP of 'Foo' for an example.) I'm not a fan of the 'new' to hide the property although thanks to AddOwner, it does point to the same shared instance so I guess it's ok. It looks like it inherits all the other metadata values as well so that's good. Just wondering if this is correct?
public class TestControlBase : Control
{
public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
"Foo",
typeof(int),
typeof(TestControlBase),
new FrameworkPropertyMetadata(4) // Original default value
);
public int Foo
{
get { return (int)GetValue(FooProperty); }
set { SetValue(FooProperty, value); }
}
}
public class TestControl : TestControlBase
{
public static readonly new DependencyProperty FooProperty = TestControlBase.FooProperty.AddOwner(
typeof(TestControl),
new FrameworkPropertyMetadata(67) // New default for this subclass
);
}
Mark
UPDATE...
I think this is even better as it eliminates the 'new' call. You still access it via the FooProperty on the base class since this uses AddOwner. As such, it's technically the same one.
public class TestControl : TestControlBase
{
// Note this is private
private static readonly DependencyProperty AltFooProperty = TestControlBase.FooProperty.AddOwner(
typeof(TestControl),
new FrameworkPropertyMetadata(67) // New default for this subclass
);
}
The correct way to override a base class's property is:
static TestControl() {
FooProperty.OverrideMetadata(
typeof(TestControl),
new FrameworkPropertyMetadata(67)
);
}
EDIT:
AddOwner is meant to share the same DependencyProperty across types that are not related (i.e. the TextProperty of TextBox and TextBlock).
I'm not quite sure if I've got the right grasp on this or not, what I've read seems to agree with what I'm trying to do, however It doesn't seem to be working.
If I add an additional owner to a dependency property of a class, whenever the orig class dp changes, the change should get propagated to the additional owner, correct?
What I have is a custom control, which I want to set a property on, and then on certain objects that are within the custom control data template inherit this property value.
public class Class1: DependencyObject{
public static readonly DependencyProperty LongDayHeadersProperty;
public bool LongDayHeaders {
get { return (bool)GetValue(LongDayHeadersProperty); }
set { SetValue(LongDayHeadersProperty, value); }
}
static Class1(){
LongDayHeadersProperty = DependencyProperty.Register("LongDayHeaders", typeof(bool), typeof(Class1),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
}
}
public class Class2: DependecyObject{
public static readonly DependencyProperty LongDayHeadersProperty;
public bool LongDayHeaders{
get{ return(bool)GetValue(LongDayHeadersProperty); }
set{ SetValue(LongDayHeadersProperty, value); }
}
static Class2(){
LongDayHeadersProperty = Class1.LongDayHeadersProperty.AddOwner(typeof(Class2));
}
}
But if I assign a DependencyPropertyDescriptor to both properties, it only fires for the Class1 and Class2 doesn't change.
Have I missed something in my understanding?
UPDATE
After some testing, I'm not even sure if my child control is considered a child control within the logical or visual tree. I think it is, but the lack of success leads me to believe otherwise.
There a many class2's which exist in an observable collection of class1. This, to me, makes them childs of class1? But even if I use RegisterAttach on class2, and set the property in class1, it doesn't seem to have any effect?
As MSDN states, the Inherits flag only works when you use RegisterAttached to create the property. You can still use the property syntax for the property.
Update
For clarity, here is how I would define the properties:
public class Class1 : FrameworkElement
{
public static readonly DependencyProperty LongDayHeadersProperty =
DependencyProperty.RegisterAttached("LongDayHeaders",
typeof(bool),
typeof(Class1),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits));
public bool LongDayHeaders
{
get { return (bool)GetValue(LongDayHeadersProperty); }
set { SetValue(LongDayHeadersProperty, value); }
}
}
public class Class2: FrameworkElement
{
public static readonly DependencyProperty LongDayHeadersProperty =
Class1.LongDayHeadersProperty.AddOwner(typeof(Class2));
public bool LongDayHeaders
{
get{ return(bool)GetValue(LongDayHeadersProperty); }
set{ SetValue(LongDayHeadersProperty, value); }
}
}
If you want your children to be logical children of your control, you need to call the AddLogicalChild. Also, you should expose them through the LogicalChildren property. I must also point out that both classes must derive from FrameworkElement or FrameworkContentElement, as the logical tree is only defined for these elements.
Since you are using an ObservableCollection, you would handle the collection changed events and Add/Remove the children depending on the change. Also, the LogicalChildren property can just return your collection's enumerator.
You are confusing DependencyProperties with Attached (Dependency) Properties.
A DP is for when a class wants bindable, stylable etc properties on itself. Just like .NET properties, they are scoped within their classes. You can register for a property changed event on individual objects, but not globally. TextBox.Text is an example of this. Note that Label.Text is unrelated to TextBox.Text.
An AP is for when a class wants to decorate another object with additional properties. The class that declares the AP is able to listen for property changed events on ALL instances of other objects that have this AP set. Canvas.Left is an example of this. Note that you always have to qualify this setter: <Label Text="Hi" Canvas.Left="50"/>
I'm creating an attached behavior in order to set a regular property of a class:
public class LookupHelper
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource", typeof(object), typeof(LookupHelper), new UIPropertyMetadata(null, OnItemsSourceChanged));
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as MyControl;
if(control == null)
return;
control.ItemsSource = (IEnumerable)e.NewValue;
}
public static object GetItemsSource(GridColumn column)
{
return column.GetValue(ItemsSourceProperty);
}
public static void SetItemsSource(GridColumn column, object value)
{
column.SetValue(ItemsSourceProperty, value);
}
}
Here, ItemsSource property on MyControl is a regular property, so I can not bind it in Xaml, hence this attached behavior.
Now, when I use this attached property using string or objects it works and breakpoint I set is hit, but when I set it with Binding markup, it never runs. Why isn't this working?
<MyControl ctrl:LookupHelper.ItemsSource="DataSource"/>; //It works
<MyControl ctrl:LookupHelper.ItemsSource="{Binding Path=MyDataSource}"/>; //Does not work
What I need to do is to set the ItemsSource property to the value specified by the Binding.
In your Get and Set methods, you're defining the receiving object as GridColumn where it should be DependencyObject.
You might also want to change the type of your DP from object to IEnumerable since your casting to that in your change handler.
Can you please post the markup you are using? Also, If the actual property exists on an object and makes sense there then I think you should be using a regular dependency property on that object instead of an attached property on a helper class.
Edit
From MSDN:
The signature for the GetPropertyName accessor must be:
public static object GetPropertyName(object target)
and the signature for the SetPropertyName accessor must be:
public static void SetPropertyName(object target, object value)
In your case, is GridColumn the correct target type?