[英]Binding a method to a delegate type DependencyProperty
在myUserControl.xaml.cs中,我有一個名為'IsExtensionValid'的布爾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
。 我嘗試了多種方法的組合,包括使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}"/>
編輯:我嘗試過的另一件事是創建一個委托類型作為mainwindow的屬性,並將其設置為引用ValidateMinFile
的靜態版本
FileSelectGroup.ExtraValidation ValidateMinFileDelegate = ValidateMinFile;`
使用ValidateMinFile
靜態,但是當我在myUserControl中訪問FileValidator的行處斷點時( bool b = FileValidator...
),FileValidator為null。
如何將窗口本地的功能綁定到該窗口中包含的UserControl的DependencyProperty? 或在這種情況下: 如何通過xaml設置myUserControlInstance.FileValidator = ValidateMinFile
?
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}}"
/>
工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.