Frage

I'm using Caliburn.Micro in my app. What I want to do is:

  • Create one RadioButton per available licence in the View
  • Check the one whose licence is currently active

So far I have two properties on my ViewModel (I'm leaving out INotify...Changed and its implementations here because that works):

BindableCollection<LicenceInfo> AvailableLicences { get; set; }
LicenceInfo ActiveLicence { get; set; }

In the ViewModel's constructor, I populate AvailableLicences and ActiveLicence. So far, so good.

Currently in the View itself, I have an ItemsControl which contains the RadioButtons and an invisible FrameworkElement to pass to MyConverter, where I extract the DataContexts of Self and the invisible FrameworkElement (whose DataContext is bound to the ViewModel) and compare them with (overridden) LicenceInfo.Equals():

<FrameworkElement Name="ActiveLicence" Visibility="Collapsed" />
<ItemsControl Name="AvailableLicences">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton cal:Message.Attach="[Event Checked] = [Action ChangeActiveLicence($dataContext)]">
                 <RadioButton.IsChecked>
                     <MultiBinding Converter="{StaticResource MyConverter}" Mode="OneWay">
                         <Binding RelativeSource="{RelativeSource Self}" />
                         <Binding ElementName="ActiveLicence" />
                     </MultiBinding>
                 </RadioButton.IsChecked>
                 [...]

This actually works as intended, but it seems to me like an ugly workaround and I'm sure that I'm missing something.

Using <Binding x:Name="ActiveLicence" /> or <Binding Path="ActiveLicence" /> as the second parameter and removing the invisible FrameworkElement does not work, the ViewModel property is not being attached to the binding.

I'm not necessarily tied to using a MultiBinding. Anything similar to the Caliburn.Micro action like the one handling the Checked event would be welcome too. Any ideas?

War es hilfreich?

Lösung

From my point of view, you're pretty close to a good solution here, if adding a flag on the LicenceViewModel is not an option:

Instead of using the container framework element, try the following multi binding:

<MultiBinding Converter="{StaticResource MyConverter}" Mode="OneWay">
    <Binding Path="DataContext" RelativeSource="{RelativeSource Self}" />
    <Binding Path="DataContext.ActiveLicense" RelativeSource="{RelativeSource FindAncestor, AncestorType=ItemsControl}" />
</MultiBinding>

Modify the converter to compare two objects using Equals(), agnostic of the concrete type. That way, you're not messing around with unnecessary objects, still separating Views and ViewModels properly.

EDIT:

Regarding the alternative solution with a flag: I didn't notice, there is no LicenseViewModel involved in your code... Adding a flag to License info is not a good solution, I agree. You can consider to wrap the LicenseInfos inside LicenseInfoViewModels, though this would require a bit of infrastructure for the synchronization between the original collection of LicenseInfos on the model and the collection containing the ViewModels.

I have posted an extensive answer on that topic here.

Then you could set the flag of the active license's ViewModel to true and all others to false, when the ActiveLicense property changes.

It's a question of the specific context, whether it makes sense to go the extra mile here. If you don't plan to extend features over time etc, and it's just a simple selection of licenses, the first solution is sufficient, probably.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top