Dependency Property to Dependency Property Binding - c#

I want to bind two Dependency Property, both in one instance of object or different instance of object or different type of instance of object.
So, i read the docs:
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-a-binding-in-code?view=netframeworkdesktop-4.8
But i see the method is:
Set source:
set Path string which is a name of instance property with user defined type
set that source instance of object which contain that property which have that name
Set destination:
set destination instance of destination dependency property
set real static dependency property
So, my question is: Is exists any method to set two Dependency Property bind look like this:
SetBindingMethod(sourceInstanceOfObject, sourceDependencyProperty, destinationInstanceOfObject, destinationDependencyProperty)
Here is my test code is working, but not that what i want:
class TestClass : DependencyObject
{
public int Int0
{
get { return (int)GetValue(Int0Property); }
set { SetValue(Int0Property, value); }
}
public int Int1
{
get { return (int)GetValue(Int1Property); }
set { SetValue(Int1Property, value); }
}
public static readonly DependencyProperty Int0Property =
DependencyProperty.Register(nameof(Int0), typeof(int), typeof(TestClass), new PropertyMetadata(0, new PropertyChangedCallback(Int0Changed)));
public static readonly DependencyProperty Int1Property =
DependencyProperty.Register(nameof(Int1), typeof(int), typeof(TestClass), new PropertyMetadata(0, new PropertyChangedCallback(Int1Changed)));
private static void Int0Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("Int0 changed: " + e.NewValue);
}
private static void Int1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Console.WriteLine("Int1 changed: " + e.NewValue);
}
internal void Test()
{
while(true)
{
Thread.Sleep(500);
Int0 += 1;
Thread.Sleep(500);
Int1 -= 1;
}
}
public TestClass()
{
Binding binding = new Binding(nameof(Int0));
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this, Int1Property, binding);
}
}
I hope that is clear, i'm on my limit of my english features :)
br: ExtSol

Related

Trigger Dependency Property Changed when child Dependency Property has been changed

I have the following class:
public class dm_fourvalues : DependencyObject
{
[JsonProperty]
public double First
{
get { return (double)GetValue(FirstProperty); }
set
{
SetValue(FirstProperty, value);
}
}
public static readonly DependencyProperty FirstProperty =
DependencyProperty.Register("First", typeof(double), typeof(dm_fourvalues),
new FrameworkPropertyMetadata(1d,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnFirstPropertyChanged)));
private static void OnFirstPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
dm_fourvalues uci = sender as dm_fourvalues;
if (uci != null)
{
uci.OnFirstChanged();
}
}
private void OnFirstChanged()
{
Console.WriteLine("first on dm fourvalues changed");
}
Now if use this class as identical property on another object, when the First property gets changed,
this object's OnChanged is not triggered, thus also a binding would not work.
What defines that a parent dependencyobject has been changed?

C# WPF Binding value does not trigger animation to in user control

Hello I have issue with binding to user control animation, after I bind data to user control(which is bool type) it sets correct values to user control data, but does not trigger animation, I tried to use PropertyChangedCallback but with no luck user control code below:
private static Switch_box AppWindow;
public Switch_box()
{
InitializeComponent();
AppWindow = this;
}
public static readonly DependencyProperty CheckboxStatusProperty = DependencyProperty.Register(nameof(CheckboxStatus), typeof(bool), typeof(Switch_box), new PropertyMetadata(false, new PropertyChangedCallback(OnCurrentReadingChanged)));//cant remove static otherwise throws error
public bool CheckboxStatus
{
get
{
return (bool)GetValue(CheckboxStatusProperty);
}
set
{
/* if (value == true)
{
((Storyboard)FindResource("OnChecking")).Begin(this);
}
else
{
((Storyboard)FindResource("OnUnchecking")).Begin(this);
}*/
SetValue(CheckboxStatusProperty, value);
}
}
private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)//cant remove static due to PropertyChangedCallBack requires static otherwise it throws error
{
AppWindow.OnChecking((bool)d.GetValue(CheckboxStatusProperty));
}
private void OnChecking(bool Status)
{
switch (Status)
{
case true:
{
((Storyboard)FindResource("OnChecking")).Begin(this);
break;
}
case false:
{
((Storyboard)FindResource("OnUnchecking")).Begin(this);
break;
}
}
}
And my usercontrol bind line:
<local:Switch_box Tag="{Binding Index,IsAsync=True}" Checked="Switch_box_Checked" Unchecked="Switch_box_Unchecked" CheckboxStatus="{Binding IsEnabled,IsAsync=True}"/>
How to trigger animation after CheckboxStatus variable is changed?
EDIT 1: updated code.
There is a naming convention. _StatusBox should be named CheckboxStatusProperty, and it should be public:
public static readonly DependencyProperty CheckboxStatusProperty =
DependencyProperty.Register(
nameof(CheckboxStatus), typeof(bool), typeof(Switch_box),
new PropertyMetadata(false, OnCurrentReadingChanged));
You must not call anything else than GetValueand SetValue in the CLR wrapper of a dependency property. And you call the methods on the current instance, not on a static field:
public bool CheckboxStatus
{
get { return (bool)GetValue(CheckboxStatusProperty); }
set { SetValue(CheckboxStatusProperty , value); }
}
In the PropertyChangedCallback it is pointless to set the property another time. And again, you should operate on the current DependencyObject instance, i.e. d, not on a static field:
private static void OnCurrentReadingChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((Switch_box)d).OnChecking((bool)e.NewValue);
}
private void OnChecking(bool status)
{
if (status)
{
((Storyboard)FindResource("OnChecking")).Begin(this);
}
else
{
((Storyboard)FindResource("OnUnchecking")).Begin(this);
}
}

How to update one dependency property from another in UWP

I have two dependency properties and when value in First changes, I would like to update also the Second - for example give it temporary value and update UI, but I don't want to break the binding on Second property.
public bool Frist
{
get { return (bool)GetValue(FristProperty); }
set { SetValue(FristProperty, value); }
}
public static readonly DependencyProperty FristProperty =
DependencyProperty.Register("Frist", typeof(bool), typeof(ItemControl), new PropertyMetadata(false, FirstUpdated));
private static void FirstUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var theControl = d as ItemsControl;
// I would like to update also second dependency property here
// for example if First == true, then Second = Overriden
// else Second = the value from binding
}
public string Second
{
get { return (string)GetValue(SecondProperty); }
set { SetValue(SecondProperty, value); }
}
public static readonly DependencyProperty SecondProperty =
DependencyProperty.Register("Second", typeof(string), typeof(ItemsControl), new PropertyMetadata(string.Empty));
I've taken a look at various samples from WPF, but seems like there are missing things in UWP. What are the options?

WFP Binding List oc Object to a Custume Control

I am trying to bind a device object List to a costume control i am working on. I getting this error.
A 'Binding' cannot be set on the 'Devices' property of type
'CamaraSelection'. A 'Binding' can only be set on a DependencyProperty
of a DependencyObject.
xml code
<trainControl:CamaraSelection Devices="{Binding DeviceList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
Control Code
private List<Device> devices = new List<Device>();
public static readonly DependencyProperty DeviceListProperty =
DependencyProperty.Register("DeviceList", typeof(List<Device>), typeof(CamaraSelection),
new PropertyMetadata(default(ItemCollection), OnDeviceListChanged));
private static void OnDeviceListChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var camaraSelection = dependencyObject as CamaraSelection;
if (camaraSelection != null)
{
camaraSelection.OnDeviceListChanged(dependencyPropertyChangedEventArgs);
}
}
private void OnDeviceListChanged(DependencyPropertyChangedEventArgs e)
{
}
public List<Device> Devices
{
get { return (List<Device>)GetValue(DeviceListProperty); }
set { SetValue(DeviceListProperty, value); }
}
The property where the binding is set on has to be a DependencyProperty. In your case it's the Devices-property. The first argument in the DependencyProperty.Register() method has to be the name of your property. The first argument in your code is "DeviceList" but your Property's name is Devices.
public static readonly DependencyProperty DevicesProperty =
DependencyProperty.Register("Devices", typeof(List<Device>), typeof(CamaraSelection),
new PropertyMetadata(default(ItemCollection), OnDeviceListChanged));
public List<Device> Devices
{
get { return (List<Device>)GetValue(DevicesProperty ); }
set { SetValue(DevicesProperty, value); }
}
"Devices" Property in your class must be a dependency property not the "DeviceList". The property which you are binding to must be a dependency property.

Clearing out a Dependency Property

I'm having some issues updating a dependency property from a standard property.
If I set the property to null or update it with new data I was under the impression that it would reset the dependency property. However it looks as tho it just piles the new data on top.
Here's a couple of properties / dependency properties I'm using:
Dependency Properties
public static readonly DependencyProperty DataTableChartProperty = DependencyProperty.Register
("DataTableChart", typeof(DataTable), typeof(MainWindowViewModel));
public static readonly DependencyProperty ContentElementProperty = DependencyProperty.Register
("ContentElement", typeof(FrameworkElement), typeof(MainWindowViewModel));
Standard Properties
public DataTable DataTableChart
{
get { return (DataTable)this.GetValue(DataTableChartProperty); }
set { this.SetValue(DataTableChartProperty, value); }
public FrameworkElement ContentElement
{
get { return (FrameworkElement)this.GetValue(ContentElementProperty); }
set { this.SetValue(ContentElementProperty, value); }
}
I would greatly appreciate any suggestions. Thanks in Advance!
This is how I'm setting it for now... for testing...
void _bw_DoWork(object sender, DoWorkEventArgs e)
{
var loadLog = new LoadLog();
e.Result = loadLog.LoadCaseLogs(SelectedFiles);
}
void _bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataTableChart = null;
ContentElement = null;
ContentElement = blah;
DataTableResult = e.Result as DataTable;
DataTableChart = caseData.LoadUserData(DataTableResult);
LoadingScreen = false;
}
public ChartControl blah = new ChartControl();
Dependency properties are cleared using the ClearValue method, setting it to null is just setting it to null, which is not the same.

Categories

Resources