English 中文(简体)
硬性使用控制
原标题:Cant use control s dependency property
  • 时间:2011-11-17 08:19:40
  •  标签:
  • c#
  • wpf

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

然后,我试图对这种依赖性财产进行约束:

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

但是,没有任何 ha、我的控制没有初步案文、没有为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本身的财产,而控制该财产时父母必须控制数据。 例如,当我使用简单文本框时:

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

所有人都是工作罚款。

最佳回答

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.

问题回答

no initial text shown in my control

我理解你在习惯控制下处置这一财产,但你是否更新了你习惯控制中的一些控制,其价值是你的扶养财产?

You may need to attach a callback and show the value set in your DP in some control in your customcontrol.

和:

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

如果已经这样做,第二个问题是具有约束力的错误。 为了确定您对来源财产的物体的控制数据。

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

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       
    }   




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签