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 ?
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With