无法使用控件的依赖属性

发布于 2024-12-16 00:06:03 字数 1122 浏览 0 评论 0原文

我的自定义控件名为“FileSelectDialog”,具有依赖属性:

    public static readonly DependencyProperty FilePathProperty =
         DependencyProperty.Register("FilePath", typeof(string), typeof(FileSelectDialog));

    public string FilePath
    {
        get { return (string)GetValue(FilePathProperty); }
        set { SetValue(FilePathProperty, value); }
    }

然后我尝试像这样绑定到此依赖属性:

    <controls:FileSelectDialog FilePath="{Binding FolderName}"/>

但没有任何反应,控件中没有显示初始文本,没有更新的文本保存到“FolderName”属性!我在输出窗口中遇到这样的错误:

    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')

因此,据我了解,控件尝试在自身上查找属性“FolderName”,而它必须在父控件 DataContext 中查找它。例如,当我使用简单的文本框时:

    <TextBox Text="{Binding Path=FolderName}"/>

一切正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我只土不豪 2024-12-23 00:06:03

对我来说似乎是一个基本的 DataContext 问题
您如何设置 FileSelectDialog 控件的 DataContext ?似乎您在代码中将 dataContext 设置为“Me”/“this”,或者在 xaml 中将 dataContext 设置为“RelativeSource Self”或类似的内容。

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.

献世佛 2024-12-23 00:06:03

我的控件中没有显示初始文本

我知道您正在自定义控件中公开此属性,但是您是否使用依赖项属性中设置的值更新自定义控件中的某些控件?

您可能需要附加回调并在自定义控件的某些控件中显示 DP 中设置的值。

就像:

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;
    }

如果已经这样做了,那么第二个问题就是绑定中显示的错误。为此,请尝试将控件的 DataContext 设置为具有 source 属性的对象。

    <controls:FileSelectDialog x:Name="customControl" FilePath="{Binding FolderName}"/> 
   ... code-behind.
     customControl.DataContext = sourceObject.

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.
小嗷兮 2024-12-23 00:06:03

你必须witre属性改变回调函数

   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       
    }   

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       
    }   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文