16

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 Action. As far as I know is the BooleanToVisibilityConverter only used when Binding is explicitly used in Caliburn and not automatically like the guard method. So I was thinking to modify the source to bind automatically to "bool? ControlNameIsVisible()" (null equals collapse) or similar. I was wondering if that is the right approach and if soo if somebody has done the implementation already and could share it here.

ride4sun
  • 169
  • 1
  • 3

2 Answers2

18

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>
devdigital
  • 34,151
  • 9
  • 98
  • 120
9

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));
            }
        }
    }
Community
  • 1
  • 1
Malcolm
  • 1,239
  • 1
  • 14
  • 25
  • 1
    Note that this doesn't work for Caliburn.Micro's deep property binding (e.g. Person_ControlName). – Govert Jan 21 '14 at 14:45
  • This works wonderfully with ToolBars. I can show/hide entire Toolbars in a ToolBarTray the same way I can enable/disable individual items in a ToolBar. Very nice! So far, there are no issues. The more I use Caliburn.Micro the less Visual Studio seems magical. – metaomniliquant Sep 10 '14 at 15:47