Visibility Autobinding with naming convention

后端 未结 2 1047
长情又很酷
长情又很酷 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>(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));
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-07 09:07

    You could use this approach if you wanted, it's perfectly reasonable. Another approach is to use a Border with the same name as a boolean property on your view model. Caliburn.Micro will set the visibility of the Border based on the value of the boolean property.

    <Border x:Name="ControlIsVisible">
      <TextBox x:Name="MyControl" ... />
    </Border>
    
    0 讨论(0)
提交回复
热议问题