Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

Edited Unassigned: Provide Parameter.Owner property protected access [328]

$
0
0
The Parameter.Owner property is marked as private, so a derived class is not able to access the Owner ActionMessage, thus cannot invoke an availability update.

To make clear why such feature is desirable, consider the following interface for a view-model:
```
public interface ISelector
{
bool CanSelect(IEnumerable items);
void Select(IEnumerable items);
}
```
The Select method is used in an ActionMessage, whose Parameter.Value is databound to a Selector.SelectedItems property. Something along these lines:
```
<UserControl>
<!-- ... some more XAML -->
<ListBox x:Name="listBox"/>
<!-- ... some more XAML -->
<Button>
<i:Interaction.Triggers>
<EventTrigger EventName="Click">
<cm:ActionMessage MethodName="Select">
<cm:Parameter Value="{Binding ElementName=listBox, Path=SelectedItems}"/>
</cm:ActionMessage>
</EventTrigger>
</i:Interaction.Triggers>
</Button>
<!-- ... some more XAML -->
</UserControl>
```
The action guard is re-evaluated only when the parameter value changes (just once), but I would like it to be re-evaluated whenever either the parameter changes __OR__ the collection changes.
To do this, I need to subclass the Parameter class, but the private access to the Owner property forced me to use reflection on a private field...
```
/// <summary>
/// Gets the action message owner.
/// </summary>
/// <returns>The action message owner.</returns>
private ActionMessage GetOwner()
{
return (ActionMessage)typeof(Parameter).GetProperty("Owner", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this, null);
}
```
If the Owner property was actually protected, I could reach the goal without this dirty hack, and the AdvancedParameter class could be used event in partial-trust.

Viewing all articles
Browse latest Browse all 1760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>