Binding to dependencyproperty on a per object basis - c#

I have the following class schema
public Class Test : DependencyObject
{
private DependencyProperty _thickness = DependencyProperty.Register("Thickness", typeof(double), typeof(CounterDataStreamWrapper));
public double Thickness
{
get
{
return (double)GetValue(this._thickness);
}
set
{
SetValue(this._thickness, value);
}
}
... Rest of the code
}
Essentially I have a collection of Test objects, and I want to bind the Thickness value for each one to its corresponding UI element. I am not too familiar with C# binding. When I try to create multiple objects, I am running into "DependencyProperty is already registered" issue. I am sure that I am just missing some key concept for binding to DependencyProperty.
Any help is appreciated!

You are registering the Thickness DependencyProperty on the CounterDataStreamWrapper type and private per instance.
Make the DependencyProperty public static and register it for the class Test.
public static DependencyProperty Thickness =
DependencyProperty.Register("Thickness", typeof(double), typeof(Test));

It's supposed to be static. Like this:
private static DependencyProperty _thickness ...

Related

BindingProxy for XAML in Xamarin

I have previously used the following code I found somewhere to implement a BindingProxy in WPF.
public class BindingProxy : Freezable
{
public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
public object Data
{
get { return this.GetValue(DataProperty); }
set { this.SetValue(DataProperty, value); }
}
}
I have tried to create a similar class for my Xamarin application which looks like this.
public class BindingProxy : BindableObject
{
// Using a DependencyProperty as the backing store for Data.
// This enables animation, styling, binding, etc...
public static readonly BindableProperty DataProperty = BindableProperty.Create(nameof(Data), typeof(object), typeof(BindingProxy), null);
public object Data
{
get { return this.GetValue(DataProperty); }
set { this.SetValue(DataProperty, value); }
}
}
This is the closest I could come up with to the Freezable class in Xamarin, unfortunately however when I declare my xaml as so
<ContentPage.Resources>
<binding:BindingProxy x:Key="BindingProxy" Data="{Binding BindingContext}" />
</ContentPage.Resources>
the Data property is never set to the BindingContext (or any other value) and returns the default value (null).
Can anyone provide any insight to what I might be doing wrong?
According to the answers here resources are not provided with the BindingContext and therefore do not support databinding.
Depending on your use case there might be another option to get the specific BindingContext, so you maybe want to explain that a little.

WPF Dependency Property On Change Update Control

I am currently trying to get a view to update when a dependency value changes.
I have copied the code from the view into it's parent and didn't use the dependency and it worked fine. I believe my issue is with how I am creating the DependencyProperty.
public partial class CULabelConfigControl : UserControl {
private CreditUnion CU { get; set; }
public static readonly DependencyProperty CUProperty = DependencyProperty.Register(
"CU",
typeof(CreditUnion),
typeof(CULabelConfigControl),
new FrameworkPropertyMetadata(null)
);
I currently receive an Error at run time:
"A 'Binding' cannot be set on the 'CU' property of type 'CULabelConfigControl'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject."
Any point in the right direction would be helpful. And let me know if I need to share any other details.
It should look like this:
public partial class CULabelConfigControl : UserControl
{
public static readonly DependencyProperty CUProperty =
DependencyProperty.Register(
nameof(CU),
typeof(CreditUnion),
typeof(CULabelConfigControl));
public CreditUnion CU
{
get { return (CreditUnion)GetValue(CUProperty); }
set { SetValue(CUProperty, value); }
}
}
In the XAML of your UserControl, you would bind to this property by specifying the UserControl as RelativeSource, e.g.
<Label Content="{Binding CU, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
If you need to get notified in the UserControl class whenever the property value changes, you should register a PropertyChangedCallback:
public static readonly DependencyProperty CUProperty =
DependencyProperty.Register(
nameof(CU),
typeof(CreditUnion),
typeof(CULabelConfigControl),
new PropertyMetadata(CUPropertyChanged));
private static void CUPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var control = (CULabelConfigControl)obj;
// react on value change here
}

Correct way to check for property change in design mode C# WPF?

Ok, so I'm trying to run some code that modifies a UI when a user changes a custom control's dependency property value in design mode; but only when in design mode.
I've tried these approaches:
1.
public static DependencyProperty x = ...Register(..., new PropertyMetadata(null, changeMethod));
2.
set { SetValue(XProp, value); changeMethod(value); }
3.
var observable = x as INotifyPropertyChanged;
observable.PropertyChanged += ObservablePropertyChanged;
But all of them seem to have their own issues in that they either trigger errors or don't work at all.
So does anyone know what the correct way to listen to a dependency property change in design mode is, and if so can you give an example?
The right way to handle DependencyProperty changes is to:
. Declare the DependencyProperty:
public static DependencyProperty MyXProperty;
. Create the public get/set Property:
public string MyX
{
get { return (string)GetValue(MyXProperty); } //Supposing that the property type is string
set { SetValue(MyXProperty, value); }
}
. Register the DependencyProperty in your static constructor:
static MyClass()
{
MyXProperty= DependencyProperty.Register("MyX", typeof(string), typeof(MyClass), new FrameworkPropertyMetadata("", OnMyXPropertyChanged));
}
. Declare the Property Changed Method:
private static void OnMyXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyClass thisClass = d as MyClass ;
//Do Something
}
Please provide more information if you still can't find your solution.

How to Expose multiple DependencyProperties (with same Name) of nested Controls in a UserControl?

I tried to solve almost same problem: "How to Expose a DependencyProperty of a Control nested in a UserControl?"
The difference is that I have different (2 or more) nested Controls with of the same type. My goal is to make the nested DependencyProperties bindable. The main Problem I am facing is that Binding don't uses the the Getter and Setter of the CLR-Property but the String of the registererd DependencyProperty. With 2 (or more) nested Controls I am facing a naming conflict.
To illustrate my Problem here the Code of the outer UserControl:
public partial class OuterControl : UserControl
{
public OuterControl()
{
InitializeComponent();
}
public Visibility PropOfInnerControl
{
get { return (Visibility)GetValue(PropOfInnerControlProperty); }
set { SetValue(PropOfInnerControlProperty, value); }
}
// Using a DependencyProperty as the backing store for UserControl2Visibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PropOfInnerControlProperty =
InnerControl.PropOfInnerControlProperty.AddOwner(typeof(OuterControl), new FrameworkPropertyMetadata(MyVisibilityPropertyChanged));
private static void MyVisibilityPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var OuterControl = obj as OuterControl;
OuterControl.InnerControl1.PropOfInnerControl = (Visibility)e.NewValue;
}
}
If it's not clear: In my example the DependencyProperty of the InnerControl1 is called PropOfInnerControlProperty and is registerd under the String "PropOfInnerControl".
In my example Binding with the InnerControl1 works fine. But I don't know how to resolve the same Problem with the InnerControl2.

Strange behavior for WPF attached property and binding

I am trying to register an WPF attached property on a grid control, however, I met very strange behavior today:
public static class MyClass
{
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(string),
typeof(MyClass), null);
public static string GetMyProperty(DependencyObject d)
{
return (string)d.GetValue(MyPropertyProperty);
}
public static void SetMyProperty(DependencyObject d, string value)
{
d.SetValue(MyPropertyProperty, value); //<-- set breakpoint here
}
}
XAML:
<GridControl local:MyClass.MyProperty="My Name">
...
</GridControl>
when I wrote like this, the attached property's setter never gets executed. and value never be set. but I can snoop into the grid and found the attached property is attached with an empty value.
but when I change the attached property name to:
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("xxxMyProperty", typeof(string),
typeof(MyClass), null);
i.e. use a different name other than MyProperty. then the breakpoint can be hit! and value can be set!
Moreover, when I change the attached property as:
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(string),
typeof(UIElement), null);
i.e. change the owner type to UIElement, then I can also hit the breakpoint, just wondering why?
However, when I setup a binding in XAML instead of a string constant, each case above will got an exception saying A 'Binding' can only be set on a DependencyProperty of a DependencyObject
Binding XAML example:
<GridControl local:MyClass.MyProperty="{Binding MyStringValue}">
...
</GridControl>
Does anyone met this strange behavior before? What am I missing in my case? Thanks in advance for the reply!
If you are referring to the SetMyProperty method as the 'setter', then you should know that these methods are simply 'helper' methods for you to use. The Framework does not generally use these methods.
If however, you are saying that you would like to know when the value changes, then there is another way of doing this. Add a PropertyChangedCallback handler to the declaration of the property:
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(string), typeof(MyClass),
new UIPropertyMetadata(default(string.Empty), OnMyPropertyChanged));
public static void OnMyPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
string myPropertyValue = e.NewValue as string;
}

Categories

Resources