c# - 将方法绑定到委托类型 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);
其中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我试图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 进行设置?
解决方案
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}}"
/>
工作正常。
推荐阅读
- html - 调整大小时如何让我的网站正确缩放?
- java - Amazon Ec2 实例的响应时间很慢
- java - 它不会给我代码错误,MySQLSyntaxErrorException
- visual-studio-code - VSCode Minimap 不工作,是我的机器太旧了吗?
- python - 如何将html标签添加到字符串,避免嵌套标签
- python - 为什么数据框的内容会影响设置?
- apache-kafka - 如何使用 Kafka 连接输出到 GCS 中的动态目录?
- python - 如何在 Python 中使用 drawContours OpenCV 方法?
- python-3.x - 如何对numpy数组中除第一列之外的所有列进行切片?
- scala - 未找到函数的隐式