Issue with DependencyProperty binding
The problem is that you set your UserControl's DataContext to itself in its constructor:
DataContext = this;
You should not do that, because it breaks any DataContext based Bindings, i.e. to a view model instance that is provided by property value inheritance of the DataContext property
Instead you would change the binding in the UserControl's XAML like this:
<TextBox Text="{Binding SelectedFile,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
Now, when you use your UserControl and write a binding like
<userControls:FileBrowserControl SelectedFile="{Binding SelectedFile}" />
the SelectedFile property gets bound to a SelectedFile property in your view model, which should be in the DataContext inherited from a parent control.
Why is my Dependency Property Binding is not working as expected?
The Binding must be TwoWay:
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
You could declare the property such that it binds TwoWay by default:
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(
nameof(SelectedItem),
typeof(object),
typeof(DragAndDropListView),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
SelectedItemPropertyChangedCallback));
WPF dependency property binding not working
Do you set the DataContext
of the SliderControl
to itself somewhere?
Try to specify the source of the binding explicitly:
<cl:SliderControl x:Name="valvePosUC" Minimum="0" Maximum="100" Title="Valve Position"
Value="{Binding DataContext.ValvePos_SliderValue, RelativeSource={RelativeSource AncestorType=Window}}" />
XAML binding not working on dependency property?
The dependency property declaration must look like this:
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register(
nameof(Test),
typeof(string),
typeof(MyControl),
new PropertyMetadata("DEFAULT"));
public string Test
{
get { return (string)GetValue(TestProperty); }
set { SetValue(TestProperty, value); }
}
The binding in the UserControl's XAML must set the control instance as the source object, e.g. by setting the Bindings's RelativeSource
property:
<UserControl x:Class="WpfTest.MyControl" ...>
<TextBlock Text="{Binding Test,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</UserControl>
Also very important, never set the DataContext
of a UserControl in its constructor. I'm sure there is something like
DataContext = this;
Remove it, as it effectively prevents inheriting a DataContext from the UserConrol's parent.
By setting Source = DataContext
in the Binding in code behind you are explicitly setting a binding source, while in
<local:MyControl Test="{Binding MyText}" />
the binding source implicitly is the current DataContext. However, that DataContext has been set by the assignment in the UserControl's constructor to the UserControl itself, and is not the inherited DataContext (i.e. the view model instance) from the window.
Problems with data binding on dependency properties
DependencyProperty.Register("StartDateTime",
You have to Name it
DependencyProperty.Register("InitialStartDateTime",
:)
Same Problem with the EndDateTime
I hope that solves your problem
WPF Binding to UserControl´s DependencyProperty not working as expected
You didn't share enough code for anybody to recreate the issue, but reading between the lines, I'm guessing that Label
is in your UserControl
XAML. If TestValue
is a property of your UserControl
, this will probably work:
<Label Content="{Binding TestValue, RelativeSource={RelativeSource AncestorType=UserControl}}" />
However, one reason you might have done that (and had it semi-work, with literal strings) is if you made your UserControl
its own DataContext
. In that case, then the problem is that you made your UserControl
its own DataContext
. If you did that, that Binding
on the bound one is being evaluated in the context of the UserControl
, which does not have a Settings.Setting123
property.
What a control's DataContext
means, is that when you have a Binding
on one of the controls properties or inside its XAML, that's where the Binding
goes to look for the property you bind to. You're explicitly telling it to look in the wrong place.
If you make your UserControl
its own DataContext
, you can't bind anything to it. That's why you shouldn't do that. It's like one of those machines that does nothing but unplug itself from the wall. Instead, use {RelativeSource AncestorType=UserControl}
bindings as above inside the UserControl
XAML.
I shouldn't have to guess. You claim you created a minimal verifiable example, but didn't bother sharing it. If you share it, we can solve your problem with confidence.
Related Topics
How to Parse a Json String That Would Cause Illegal C# Identifiers
How to Auto-Generate a C# Class File from a Json String
Sending Email Through Gmail Smtp Server With C#
Evaluating String "3*(4+2)" Yield Int 18
In C#, Difference Between Public, Private, Protected, and Having No Access Modifier
C# String Replace Does Not Actually Replace the Value in the String
Unity Scripts Edited in Visual Studio Don't Provide Autocomplete
What Are Some Good .Net Profilers
How to Make a Textbox That Only Accepts Numbers
How to Turn a C# Object into a Json String in .Net
.Net String.Format() to Add Commas in Thousands Place For a Number
How to Write Unicode Characters to the Console