我非常喜欢Caliburn框架以及它的命名约定绑定方式,但惊讶的是,可见性并没有像“Can名称”约定那样使用绑定来保护操作。据我所知,当在Caliburn中显式地使用绑定时,才会使用BooleanToVisibilityConverter转换器,而不会像guard方法一样自动绑定。因此,我考虑修改源代码,使其自动绑定到类似于“bool? ControlNameIsVisible()”(null等于收缩)之类的内容。我想知道这是否是正确的方法,如果是,是否有人已经实现了它,并可以在这里分享。
我非常喜欢Caliburn框架以及它的命名约定绑定方式,但惊讶的是,可见性并没有像“Can名称”约定那样使用绑定来保护操作。据我所知,当在Caliburn中显式地使用绑定时,才会使用BooleanToVisibilityConverter转换器,而不会像guard方法一样自动绑定。因此,我考虑修改源代码,使其自动绑定到类似于“bool? ControlNameIsVisible()”(null等于收缩)之类的内容。我想知道这是否是正确的方法,如果是,是否有人已经实现了它,并可以在这里分享。
<Border x:Name="ControlIsVisible">
<TextBox x:Name="MyControl" ... />
</Border>
protected override void Configure()
{
base.Configure();
ConventionManager.AddElementConvention<UIElement>(UIElement.VisibilityProperty, "Visibility", "VisibilityChanged");
var baseBindProperties = ViewModelBinder.BindProperties;
ViewModelBinder.BindProperties =
(frameWorkElements, viewModel) =>
{
BindVisiblityProperties(frameWorkElements, viewModel);
return baseBindProperties(frameWorkElements, viewModel);
};
// Need to override BindActions as well, as it's called first and filters out anything it binds to before
// BindProperties is called.
var baseBindActions = ViewModelBinder.BindActions;
ViewModelBinder.BindActions =
(frameWorkElements, viewModel) =>
{
BindVisiblityProperties(frameWorkElements, viewModel);
return baseBindActions(frameWorkElements, viewModel);
};
}
void BindVisiblityProperties(IEnumerable<FrameworkElement> frameWorkElements, Type viewModel)
{
foreach (var frameworkElement in frameWorkElements)
{
var propertyName = frameworkElement.Name + "IsVisible";
var property = viewModel.GetPropertyCaseInsensitive(propertyName);
if (property != null)
{
var convention = ConventionManager
.GetElementConvention(typeof(FrameworkElement));
ConventionManager.SetBindingWithoutBindingOverwrite(
viewModel,
propertyName,
property,
frameworkElement,
convention,
convention.GetBindableProperty(frameworkElement));
}
}
}