Visibility Autobinding with naming convention

后端 未结 2 1068
长情又很酷
长情又很酷 2021-02-07 08:18

I really like Caliburn and the naming convention binding and was surprised that the Visibility is not bound in the same way the \"CanNAME\" Convention is used to guard an Acti

2条回答
  •  梦谈多话
    2021-02-07 08:57

    If you want a general solution, this is what I ended up with, based on: Adding a convention for IsEnabled to Caliburn.Micro

    Note the overriding of BindActions as well as BindProperties so that you can check for Visibility on things that have actions bound to them.

        protected override void Configure()
        {
            base.Configure();
    
            ConventionManager.AddElementConvention(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 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));
                }
            }
        }
    

提交回复
热议问题