Reputation: 1394
I have my custom control named "FileSelectDialog" with Dependency Property:
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string), typeof(FileSelectDialog));
public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
Then I'm trying to bind to this dependency property like this:
<controls:FileSelectDialog FilePath="{Binding FolderName}"/>
But nothing happing, no initial text shown in my control, no updated text's saving to 'FolderName' property! I got such error in Output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'FolderName' property not found on 'object' ''FileSelectDialog' (Name='FolderSelector')'. BindingExpression:Path=FolderName; DataItem='FileSelectDialog' (Name='FolderSelector'); target element is 'FileSelectDialog' (Name='FolderSelector'); target property is 'FilePath' (type 'String')
So, as far as I understand, control try to find property 'FolderName on itself, while it must look for it in parents control DataContext. For example, when I use simple textbox:
<TextBox Text="{Binding Path=FolderName}"/>
All is working fine.
Upvotes: 1
Views: 140
Reputation: 17499
no initial text shown in my control
I understand you are exposing this property in a custom control, but are you updating some of the control in your customcontrol with the value set in your dependency property?
You may need to attach a callback and show the value set in your DP in some control in your customcontrol.
Like:
public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(FileSelectDialog), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.None,HandleFilePathPropertyChanged));
private static void HandleFilePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control= (FileSelectDialog)d;
control.SomeUIControl.Text= (string)e.NewValue;
}
If already doing this, then the second problem is the error shown in Binding. For this try setting the DataContext of your control to the object which has the source property.
<controls:FileSelectDialog x:Name="customControl" FilePath="{Binding FolderName}"/>
... code-behind.
customControl.DataContext = sourceObject.
Upvotes: 1
Reputation: 4885
You have to witre the property changed call back function
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string),
typeof(FileSelectDialog),new UIPropertyMetadata(
new PropertyChangedCallback(PropertyChanged)));
private static void PropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
//Do your Stuff
}
Upvotes: 0
Reputation: 19294
Seems a basic DataContext issue to me How did you set the DataContext of your FileSelectDialog Control ? seems you set the dataContext in code as 'Me'/'this' or in xaml with 'RelativeSource Self' or something like this.
Upvotes: 3