Simple Question: Binding a Label to a method?

Apr 27, 2011 at 11:17 AM

Is that possible?

In my master detail I now actually want:

I have a ContentControl that contains in the DataTemplate a TextBox. The ContentControl is bound to the ActiveItem of a single active Conductor.

If possibly I would like to cal´l for data binding, when the ActiveItem in the Conductor changes, a method on the ViewModel and bind the output of that to the TextBox. This method will do some conversion internally - not totally simple, no magic either. This is also a generic query about databinding against a method. Google finds references saying it is possible using CM, but no reference I find actually has an example for that.

Apr 27, 2011 at 1:19 PM

I think that you can define a proper TriggerBase to react to an event from the VM, or use an existing EventTrigger monitoring the SelectionChanged event of the Selector the ActiveItem is bound to, to execute an ActionMessage.


<ListBox DockPanel.Dock="Right"
         ItemsSource="{Binding Path=Items.Items}"
         SelectedItem="{Binding Path=Items.ActiveItem, Mode=TwoWay}"
         cm:Action.TargetWithoutContext="{Binding Path=ActiveItem}">
                  <Label Content="{Binding Path=TypeName}" />
            <i:EventTrigger EventName="SelectionChanged">
                  <cm:ActionMessage MethodName="DoSomething" />
<ContentControl x:Name="ActiveItem"
                Content="{Binding Path=Items.ActiveItem, Mode=OneWay}" />

Note that the above code should just execute the DoSomething method on the current ActiveItem, it will not use an eventual return value from the method.

To display the result of a method on the UI, I would still define a proper VM property, populated by the invoked action.

I am not aware of a way to bind directly to the result of a function (unless you use an ObjectDataProvider... but that's another story and it is not related to CM specifically). I suppose Rob can correct me if I am wrong! :)