Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Add custom Convention for caliburn.micro?

In my project,I need to binding visibility of ui element to a bool property,as you know caliburn.micro has convention "CanName",so i think to add my own custom convention. then i found this [Visibility Autobinding with naming convention i add this code in my project,but it not work and convention "CanName" doesn't work too.

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));
                }
            }
        }

anyone know what wrong with this code ?

like image 494
tfzxyinhao Avatar asked Mar 20 '23 13:03

tfzxyinhao


1 Answers

I'm using this Convention in my project without any issues. Here is a walkthrough:

In the AppBootstrapper.cs you include the Element Convention among other given Conventions

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);
    };
}

and your helper method:

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));
        }
    }
}

In your PageView.xaml you place your Controls and provide a 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>

within your PageViewModel.cs you place a public property of type bool with the suffix IsVisible

public bool FooIsVisible { get { return true; } }

public bool BarIsVisible { get { return false; } }
like image 137
flo scheiwiller Avatar answered Apr 08 '23 03:04

flo scheiwiller