在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);
其中FileFilters
和FileValidator
分别是字符串和委托类型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 我试图通过xaml绑定myUserControlInstance.FileValidator = ValidateMinFile
。我尝试了多种方法组合,包括make ValidateMinFile
静态与否,包含或不作为资源,是否引用为RelativeSource,以及其他一些我不记得的方法。我当前的迭代(翻译成人工实例 - 土地)是:
<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
静态但当我在myUserControl中访问FileValidator(bool b = FileValidator...
的那一行)的断点时,FileValidator为空。
如何将窗口本地的函数绑定到该窗口中包含的UserControl的DependencyProperty?或在这种特殊情况下:如何设置myUserControlInstance.FileValidator = ValidateMinFile
XAML?
答案 0 :(得分:1)
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);
}
}
MainWindow.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");
}
MainWindow.xaml
<local:UserControl1
FileValidator="{Binding FileValidator, RelativeSource={RelativeSource AncestorType=Window}}"
/>
工作正常。