首页 > 解决方案 > 将方法绑定到委托类型 DependencyProperty

问题描述

在 myUserControl.xaml.cs 中,我有一个名为“IsExtensionValid”的 bool DependencyProperty,其值由以下几行分配:

    bool a = TargetFile.Extension.MatchFileMask(FileFilters, true);
    bool b = (FileValidator is null) ? true : FileValidator(TargetFile).Item1;
    IsExtensionValid = (a && b);

其中FileFiltersFileValidator分别是字符串和委托类型 DependencyProperty,其委托类型FileValidator定义为:

    public delegate Tuple<bool, string> ExtraValidation(FileInfo fileInfo);
    public delegate Tuple<bool, string> StaticExtraValidation(FileInfo fileInfo, object o);
    // I also tried this static version with corresponding modifications to the function definition and usages (see below) but still couldn't bind

在 mainwindow.xaml.cs中,我定义了一个函数:

    public Tuple<bool, string> ValidateMinFile(FileInfo f) // ExtraValidation delegate
    { return new Tuple<bool, string>(true, "File is invalid"); }
    // meaningful test logic removed but principle stands

在 mainwindow.xaml我试图myUserControlInstance.FileValidator = ValidateMinFile通过 xaml 绑定。我尝试了多种方法组合,包括是否为ValidateMinFile静态、是否作为资源包含、是否作为相对源引用,以及更多我不记得的方法。我当前的迭代(翻译为人为的示例土地)是:

<local:myUserControl x:Name="MinFileControl"
                       FileFilters="Min Files|*.min"
                       FileValidator="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:}}, Path=ValidateMinFile}"/>

真正的线路实际上是这样的:

<local:FileSelectGroup x:Name="fsgMinFile" DockPanel.Dock="Top" Margin="2"
                       Title="Min file:"
                       FileFilters="Min Files|*.min"
                       PropertyChanged="fsgMinFile_PropertyChanged"
                       FileValidator="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:mainwindow}}, Path=ValidateMinFile}"/>

编辑:我尝试过的另一件事是创建一个委托类型作为主窗口的属性并将其设置为引用的静态版本ValidateMinFile

FileSelectGroup.ExtraValidation ValidateMinFileDelegate = ValidateMinFile;`

使用ValidateMinFile静态但是当我在访问 FileValidator 的 myUserControl 中的行处断点时(带有 的bool b = FileValidator...) FileValidator 为空。


如何将窗口的本地函数绑定到该窗口中包含的 UserControl 的 DependencyProperty?或者在这种特殊情况下:如何myUserControlInstance.FileValidator = ValidateMinFile通过 xaml 进行设置?

标签: c#wpfdata-bindingdependency-properties

解决方案


UserControl1.xaml.cs

public delegate Tuple<bool, string> ExtraValidation(FileInfo fi);

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    #region FileValidator Property
    public ExtraValidation FileValidator
    {
        get { return (ExtraValidation)GetValue(FileValidatorProperty); }
        set { SetValue(FileValidatorProperty, value); }
    }

    public static readonly DependencyProperty FileValidatorProperty =
        DependencyProperty.Register(nameof(FileValidator), typeof(ExtraValidation), typeof(UserControl1),
            new PropertyMetadata(null, FileValidator_PropertyChanged));
    #endregion FileValidator Property

    protected static void FileValidator_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //  I just put this here for testing: If it's non-null, it'll be called. 
        //  I set a breakpoint in the MainWindow method to detect the call. 
        (d as UserControl1).FileValidator?.Invoke(null);
    }
}

主窗口.xaml.cs

    public MainWindow()
    {
        InitializeComponent();

        FileValidator = ValidateMinFile;
    }

    #region FileValidator Property
    public ExtraValidation FileValidator
    {
        get { return (ExtraValidation)GetValue(FileValidatorProperty); }
        set { SetValue(FileValidatorProperty, value); }
    }

    public static readonly DependencyProperty FileValidatorProperty =
        DependencyProperty.Register(nameof(FileValidator), typeof(ExtraValidation), typeof(MainWindow),
            new PropertyMetadata(null));
    #endregion FileValidator Property

    public Tuple<bool, string> ValidateMinFile(FileInfo f) // ExtraValidation delegate
    {
        //  Breakpoint here
        return new Tuple<bool, string>(false, "blah");
    }

主窗口.xaml

    <local:UserControl1
        FileValidator="{Binding FileValidator, RelativeSource={RelativeSource AncestorType=Window}}"
        />

工作正常。


推荐阅读