How to add custom convention for caliburn.micro?

In my project, I need to bind the visibility of the ui element to the bool property, since you know that caliburn.micro has a “CanName” convention, so I’m thinking of adding my own convention. then I found this [Autobinding visibility with a naming convention I add this code to my project, but it does not work, and the "CanName" convention does not work either.

ConventionManager.AddElementConvention<FrameworkElement>(Control.VisibilityProperty, "Visibility", "IsVisible");
            var baseBindProperties = ViewModelBinder.BindProperties;
            ViewModelBinder.BindProperties = (frameWorkElements, viewModel) =>
            {
                BindVisiblityProperties(frameWorkElements, viewModel);
                return baseBindProperties(frameWorkElements, viewModel);
            };


static void BindVisiblityProperties(IEnumerable<FrameworkElement> items, Type viewModel)
        {
            foreach (FrameworkElement element in items)
            {
                string PropertyName = element.Name + "IsVisible";
                var property = viewModel.GetPropertyCaseInsensitive(PropertyName);
                if (property != null)
                {
                    var convention = ConventionManager.GetElementConvention(typeof(FrameworkElement));
                    ConventionManager.SetBindingWithoutBindingOverwrite(viewModel, PropertyName, property,
                        element, convention, convention.GetBindableProperty(element));
                }
            }
        }

Does anyone know what happened to this code?

+4
source share
1 answer

I use this Convention in my project without any problems. Here's a walkthrough:

AppBootstrapper.cs

private static void AddCustomConventions()
{
ConventionManager.AddElementConvention<FrameworkElement>(Control.VisibilityProperty, "Visibility", "IsVisible");
            var baseBindProperties = ViewModelBinder.BindProperties;
            ViewModelBinder.BindProperties = (frameWorkElements, viewModel) =>
                {
                    BindVisiblityProperties(frameWorkElements, viewModel);
                    return baseBindProperties(frameWorkElements, viewModel);
                };
}

:

private static void BindVisiblityProperties(IEnumerable<FrameworkElement> items, Type viewModel)
        {
            foreach (FrameworkElement element in items)
            {
                string PropertyName = element.Name + "IsVisible";
                var property = viewModel.GetPropertyCaseInsensitive(PropertyName);
                if (property != null)
                {
                    var convention = ConventionManager.GetElementConvention(typeof(FrameworkElement));
                    ConventionManager.SetBindingWithoutBindingOverwrite(
                        viewModel, PropertyName, property, element, convention, convention.GetBindableProperty(element));
                }
            }
        }

PageView.xaml x:Name

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button x:Name="StartRecord">start</Button>
                <Button x:Name="StopRecord">stop</Button>
                <Button x:Name="Foo">foo</Button>
                <Button x:Name="Bar">bar</Button>
            </StackPanel>

PageViewModel.cs public property bool IsVisible

public bool FooIsVisible { get { return true; } }

public bool BarIsVisible { get { return false; } }
+5

Source: https://habr.com/ru/post/1527415/


All Articles