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.
Visibility Autobinding with naming convention
Asked Answered
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>
Nicely done. It may not be the "right" solution for all instances, but this is a great tip. –
Porscheporsena
For those who do not wish to add extra control on UI, you can bind visibility directly to the property. This doesn't autobind via convention, but the code it takes is about the same as the above, and without extra control to wrap. –
Cresa
Do you have any examples? –
Bastien
@PaulMrozowski I'm guessing something like this: <TextBox x:Name="MyControl" Visibility="{Binding ControlIsVisible, Converter={StaticResource boolToVisibility}}" –
Cusped
This works extremely well. Within the Caliburn.Micro framework, I feel it also self-documents itself. I love this solution. –
Pubilis
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));
}
}
}
Note that this doesn't work for Caliburn.Micro's deep property binding (e.g. Person_ControlName). –
Karnak
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. –
Rsfsr
© 2022 - 2024 — McMap. All rights reserved.