Make ViewModel property available for binding to IsChecked - wpf

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?

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.

Related

WPF TextBlock MultiBinding

I want to bind TextBlock's Text property to some elements' and some model's properties. Something like this:
<TextBlock>
<TextBlock.Text>
<MultiBinding>
<Binding ElementName="myElement1" Mode="OneWay" Path="Text" />
<Binding ElementName="myElement2" Mode="OneWay" Path="Text" />
<Binding Mode="OneWay" Path="Property1" />
<Binding Mode="OneWay" Path="Property2" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The TextBlock has a text value, combination of myElement1, myElement2 and Property1, Property2. There is not a problem. The text value is generated successfully.
Here is my question:
Can I bind whole (combined) text value of the TextBlock to another model's property, i.e. Property3, without code?
Not without some really bad hacking which would require writing some code to set up attached properties and other bindings anyway. The issue is that any binding has 2 ends: target and source. Since the target (where the binding is set) must be a DependencyProperty that means that your model must be on the source end of the binding you're trying to do. This isn't a problem as far as setting a value since TwoWay and OneWayToSource bindings do this just fine.
You have a bigger problem though in that the original place where the value is coming from (TextBlock.Text) already is assigned a binding and so can't be the target for your model binding. You might next want to try using another UIElement property as an intermediary to take the Text value and push it to the model. To do that you again need the model to be the source and the other UIElement property to be the target. But that same property also needs to be the target of a binding to the original Text property that you're trying to extract, so again you're stuck.
Bottom line is that you're much better off handling this in your Model and ViewModel layers rather than trying to force the stuff you have set up in XAML to be driving everything.

Can I data bind to a indexed property where a property of the parent is the index

Using WPF with MVVM, my VM has an indexed property
IObject1 this[string key]
I need to bind a property of the view to a property of IObject1, and the key of the object1 that I need is the name of the control in the view. Essentially I need nested bindings
<TextBlock x:Name="Key1" Text="{Binding ["Key1"].DisplayText}
but, the name will very for different items so I need the nested binding
<TextBlock x:Name="Key1" Text="{Binding [{Binding Name, RelativeSource={RelativeSource Self}].DisplayText}
My actual case is more complicated than this, but if I can get this far I think that I can figure out the rest.
I'm using Blend, and I'd love a way to teach my designer to do this type of thing within Blend, but I'm happy to use code if I need to.
Am I overlooking something obvious? I can't figure out how to do this and I haven't stumbled upon the correct Google / Stack Overflow search term.
Thanks.
That's a weird solution lol, anyhose, you can solve it with MultiBinding & converters.
<TextBlock.Text>
<MultiBinding Converter={StaticResource combine}>
<Binding Path=Dictionary />
<Binding Path=Name />
</Multibinding>

Silverlight: Select control based on list count

I'm bind a Telerik RadGridView to a List<MyObject> myList = new List<MyObject>. But if the myList.Count == 0 (the list is empty ;) ) I want to show another control to the user.
I know I could use some visibility converter, but I prefer achieving this in XAML.
Thank you
I think that value converters are your only choice here :)
However, I've found out that if you structure them properly, value converters are great.
Here are a couple of good tools for this:
A Generic Boolean Value Converter
Linking Multiple Value Converters in WPF and Silverlight
With these tools in mind, I would go with something like this:
<Grid>
<telerik:RadGridView ItemsSource="{Binding myList}">
<telerik:RadGridView.Visibility>
<Binding Path="myList">
<Binding.Converter>
<converters:SequentialValueConverter>
<converters:IsEmptyConverter />
<converters:BooleanToVisibilityConverter TrueValue="Collapsed" FalseValue="Visible" />
</converters:SequentialValueConverter>
</Binding.Converter>
</Binding>
</telerik:RadGridView.Visibility>
</telerik:RadGridView>
<YourControl>
<YourControl.Visibility>
<Binding Path="myList">
<Binding.Converter>
<converters:SequentialValueConverter>
<converters:IsEmptyConverter />
<converters:BooleanToVisibilityConverter TrueValue="Visible" FalseValue="Collapsed" />
</converters:SequentialValueConverter>
</Binding.Converter>
</Binding>
</YourControl.Visibility>
</YourControl>
</Grid>
Also, as Jason said, myList needs to be an ObservableCollection so the gui gets notified when it changes.
Hope it helps!
If you switched to ObservableCollection<MyObject> you can bind using VisibilityConverters to that your myList.Count all in XAML. If you are having issues because you are setting the ItemsSource in codebehind, you may want to have it be a resource or switch to something more MVVM.

MultiBinding not working but corresponding Binding does work

I have the following code:
<local:StaffAtMeetingEditor DataContext="{Binding Meeting}" Grid.Row="1">
<local:StaffAtMeetingEditor.InEditMode>
<MultiBinding Converter="{StaticResource myMeetingLogEditableMultiConverter}">
<Binding Path="ParentSI.ItemInEditMode"/>
</MultiBinding>
</local:StaffAtMeetingEditor.InEditMode>
</local:StaffAtMeetingEditor>
The setup is that the containing control's datatype is "SIP_ServiceItem". This class has a property called "Meeting" (which is set as the DataContext for the local:StaffAtMeetingEditor control), which itself has a member called "ParentSI", pointing back to the parent SIP_ServiceItem object.
The issue is, that if I pass this through as a single binding (i.e. remove the start and end MultiBinding tags from the code above, leaving just the Binding), it works just fine. But when I make it a MultiBinding (I wish to add some other Bindings to this shortly), and try to pass the bound value through to myMeetingLogEditableMultiConverter, the values(0) parameter, which should correspond to the boolean ParentSI.ItemInEditMode is actually an MS.Internal.NamedObject, implying there's a null reference somewhere. Furthermore, the ParentSI property is never being evaluated, so something is going completely wrong. I am at a loss to know the difference between the single binding and multi binding cases.
Thanks.
I know this is a bit old, and you have likely figured this out by now, but I came across this as I had a similar problem and thought I'd share the solution: I had the same problem and have added the attributes ElementName and Mode as below:
<Binding Path="CurrentProvider.IsBusy" ElementName="parent" Mode="OneWay" />
Hope this helps someone, even if the OP has fixed their issue.
May be you should try to add any temporary unused bound value. For instance:
<local:StaffAtMeetingEditor DataContext="{Binding Meeting}" Grid.Row="1">
<local:StaffAtMeetingEditor.InEditMode>
<MultiBinding Converter="{StaticResource myMeetingLogEditableMultiConverter}">
<Binding Path="ParentSI.ItemInEditMode"/>
<Binding Path="ParentSI"/>
</MultiBinding>
</local:StaffAtMeetingEditor.InEditMode>
</local:StaffAtMeetingEditor>
If it doesn't work then your implementation is wrong, another case - it's MultiBinding limitations.

Built-in WPF IValueConverters

Ok, it was a nice surprise (after writing it several times) to find that there already is a BooleanToVisibilityConverter in System.Windows.Controls namespace.
Probably there are more such hidden time-savers.
Anyone got some?
I did a quick trawl using the Object Browser and this is what I have.
Derived from IValueConverter:
System.Windows.Controls.AlternationConverter
System.Windows.Controls.BooleanToVisibilityConverter
System.Windows.Documents.ZoomPercentageConverter
System.Windows.Navigation.JournalEntryListConverter
Xceed.Wpf.DataGrid.Converters.CurrencyConverter
Xceed.Wpf.DataGrid.Converters.DateTimeToStringConverter
Xceed.Wpf.DataGrid.Converters.GreaterThanZeroConverter
Xceed.Wpf.DataGrid.Converters.IndexToOddConverter
Xceed.Wpf.DataGrid.Converters.IntAdditionConverter
Xceed.Wpf.DataGrid.Converters.InverseBooleanConverter
Xceed.Wpf.DataGrid.Converters.LevelToOpacityConverter
Xceed.Wpf.DataGrid.Converters.MultimodalResultConverter
Xceed.Wpf.DataGrid.Converters.NegativeDoubleConverter
Xceed.Wpf.DataGrid.Converters.NullToBooleanConverter
Xceed.Wpf.DataGrid.Converters.SourceDataConverter
Xceed.Wpf.DataGrid.Converters.StringFormatConverter
Xceed.Wpf.DataGrid.Converters.ThicknessConverter
Xceed.Wpf.DataGrid.Converters.TypeToBooleanConverter
Xceed.Wpf.DataGrid.Converters.TypeToVisibilityConverter
Xceed.Wpf.DataGrid.Converters.ValueToMaskedTextConverter
Derived from IMultiValueConverter:
System.Windows.Controls.BorderGapMaskConverter
System.Windows.Navigation.JournalEntryUnifiedViewConverter
System.Windows.Controls.MenuScrollingVisibilityConverter
Microsoft.Windows.Themes.ProgressBarBrushConverter
Microsoft.Windows.Themes.ProgressBarHighlightConverter
Note the Xceed ones (no connection) are available free with their DataGrid. As well as those there's some clever stuff around like the debugging converter. I've also used the last IValueConverter and I'm sure there's some further lambda function goodness to be found, too.
Before 3.5 SP1, an IValueConverter was required for string formatting. Now, you can use the StringFormat property on Binding to do this.
From the MSDN page:
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} -- Now only {1:C}!">
<Binding Path="Description"/>
<Binding Path="Price"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>

Resources