Binding to current source ("{Binding .}") in WinRT - winrt-xaml

In WPF, if I want to reference the whole object (not a specific property) in a binding, I can write any of the following:
{Binding Path=.}
{Binding .}
{Binding}
Like so:
<ItemsControl ItemsSource="{Binding Widgets}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:WidgetControl Widget="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
However in WinRT I have not been able to get this to work:
<GridView ItemsSource="{Binding Source={StaticResource Widgets}}">
<GridView.ItemTemplate>
<DataTemplate>
<controls:WidgetControl Widget="{Binding}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
It fails with a very vague exception:
The text associated with this error code could not be found.
Failed to assign to property 'XX.WidgetControl.Widget'. [Line: XX Position: XX]
The ItemsSource binding works because the DataContext of each controls:WidgetControl gets set to a Widget. And I know the obvious answer to this is just to wait for the DataContext to be set and cast that to a Widget inside my widget control. But I'm asking on principle: Why doesn't self-binding work in WinRT... or is there a notation to make it work?

So it turns out {Binding} works just great. I had wrongly set the DataContext in my WidgetControl, causing the binding to fail and leading me to believe the WinRT binding mechanism was at fault.
To be clear, {Binding .} still doesn't work like it does in WPF as best as I can tell. It interprets the dot as a literal property name instead of a reference to self:
Error: BindingExpression path error: '.' property not found on 'Widget'. BindingExpression: Path='.' DataItem='Widget'
But my actual problem was trying to change the DataContext at the root level of my WidgetControl, causing any external bindings to fail. Because I defined my control as <UserControl x:Name='controlRoot' DataContext="{Binding ElementName=controlRoot}, the binding in <controls:WidgetControl Widget="{Binding}"/> was referencing the WidgetControl itself and not the Widget I was trying to pass in. And the "failed to assign to property" exception was thrown by a property changed handler that was expecting a Widget, not a WidgetControl.
So you can absolutely use {Binding} to reference the DataContext of the thing the binding is on, just don't try to set the path to ".".

Related

Updated Catel from 5.12.4 to 5.12.15: runtime error on every EventToCommand line inViews

After updating Catel from 5.12.4 to 5.12.15 every view with xaml parts like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="...">
<catel:EventToCommand Command="..." />
</i:EventTrigger>
</i:Interaction.Triggers>
causes runtime error:
An instance of type "EventToCommand" cannot be added to a collection of type "TriggerActionCollection". Only items of type 'T' are allowed.
This is caused by the change to move to the (actively maintained) Microsoft.Xaml.Behaviors package. Please use the correct package moving forward and all should be good.

How can i assign Mule Variable Value to Property Key -Value lookup?

I want to find out the careerName from variable
I want to use that CareerName as Property Key. Example. If careerName came-up as apple, I have value setup against that key apple=ST\*214|ST\*210.
I have following line of code for Mule Choice Expression, which i tried with but i am not getting success here.
mule-esb.test1.properties
ftp.inbound.carriers.path='CareerName1/InBound/','CareerName2/InBound/','CareerName3/InBound','CareerName4/InBound/','apple/InBound/'
CareerName1=ST\*214|ST\*210
CareerName2=ST\*214|ST\*210
CareerName3=.\ST.214.\
CareerName4=ST\*214
apple=ST\*214
<context:property-placeholder location="mule-esb.${mule.env}.properties" />
<when expression="import java.util.regex.Pattern;Pattern p = Pattern.compile('${'+message.getInvocationProperty('careerName')+'}');return p.matcher(payload.toString()).find();" evaluator="groovy">
Looking for some alternatives or solution on this script.
MEL has extensive regex support, you shouldn't need to use Groovy. See: http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+Tips#MuleExpressionLanguageTips-RegexSupport
You need to load your properties in an Map that you can query from the registry but also use in the property placeholder resolver. So do this:
<util:properties id="configProperties"
location="classpath:mule-esb.${mule.env}.properties" />
<context:property-placeholder properties-ref="configProperties" />
With this in place, the following should work:
<when expression="#[regex(app.registry.configProperties[careerName])]">
define a spring bean globally configuring the key value pair properties for the bean.
the bean definition should accept the spring properties and a method which accept the key and returns the corresponding value.
sample bean definition as follows
<spring:bean id="entityMapper" name="entityMapper" class="com.xx.xx.commons.ClassNameXX">
<spring:property name="entities">
<spring:props>
<spring:prop key="CareerName1">${CareerName1}</spring:prop>
.
.
</spring:props>
</spring:property>
</spring:bean>
so in the flow level you can get value from bean with below expression.
#[app.registry.entityMapper.getEntity(message.getInvocationProperty('careerName'))]
where entityMapper will be the bean name and getEntity is the method defined in bean which accept the careerName and return the corresponding value.
hope this helps.
Dynamically you cant access the value directly from context placeholder.

Unknown member 'SortDescriptions' on element 'CollectionViewSource'

This is a real newbie question. I feel dumb that I have not figured it out yet. I am trying to add a sort to my CollectionViewSource in my Win 8 App.
<CollectionViewSource
x:Name="itemsViewSource"
x:Key="cvs"
Source="{Binding Items}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="PubDate" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
I have the following namespaces declared:
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
But I get the error "Unknown member 'SortDescriptions' on element 'CollectionViewSource'" when I try to build. What am I missing?
I believe that CollecitonViewSource in WinRT does not have SortDescription. You may have to order the Items instead.
This link might help as well.
A WinRT CollectionView class with Filtering and Sorting

C# objects Xaml factory method for XamlWriter

My class uses an image as Property (public Image myImage). When this class is serialized (XamlWriter) it works fine, reading it back gives an exception:
'No matching constructor found on type
'System.Drawing.Bitmap'. You can use
the Arguments or FactoryMethod
directives to construct this type.'
Line number '5' and line position '8'.
Obviously there is a constructor missing for Bitmap with ColorPalette as argument:
<sd:Bitmap>
<sd:Bitmap.Palette>
<sdi:ColorPalette />
</sd:Bitmap.Palette>
</sd:Bitmap>
I understand that I can specify a factory method creating the bitmap somehow. I also found some articles about directives for this such as http://www.wpftutorial.net/XAML2009.html
However, I am not an expert in Xaml and do not understand how and where to define / declare the method on my attribute. Unfortunately i do also not find an example for this.
I expect some like
[FactoryMethod("XYZ")]
public Image myImage ....
but actually have found nothing. Any idea / example you know?

How do I tell ReSharper that an attribute means that a method is used?

I'm playing with SpecFlow, and ReSharper thinks that my step definitions are unused (I guess because they're used via reflection):
[Binding]
public class StepDefinitions
{
// ...
[When(#"I press add")]
public void WhenIPressAdd() // R# thinks this is unused
{
_calculator.PressAdd();
}
// ...
}
How can I tell ReSharper that methods with [Given], [When], [Then] attributes (etc.) are actually used? I don't want to use // ReSharper disable UnusedMember.Global comments.
I could also mark each method (or the whole class) with [JetBrains.Annotations.UsedImplicitly]. I don't particularly want to do that either.
You need to use JetBrains Annotations, and mark the attribute with an MeansImplicitUseAttribute. You can either reference JetBrains.Annotations.dll directly, or you can copy the annotations source code (from ReSharper / Options / Code Inspection / Code Annotations) into your solution.
If you need to annotate some external assembly you don't own, you need to create an External Annotation file (xml) in the following folder: %ReSharperInstallDir%\Bin\ExternalAnnotations. There are plenty of examples, you can just copy some.
The external annotations file can also be in the same path as the DLL if you name it DllNameWithoutExtension.ExternalAnnotations.xml.
There are plenty of examples, but I wanted to be a little more explicit in case you don't want to track down an example. :)
Create a file with the name of the attribute's assembly (.xml) in %ReSharperInstallDir%\Bin\ExternalAnnotations. For example, I made Microsoft.VisualStudio.QualityTools.CodedUITestFramework.xml and put this XML inside it:
<assembly name="Microsoft.VisualStudio.QualityTools.CodedUITestFramework">
<member name="T:Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
</member>
</assembly>
Restart VS and you're on your way!
these answers have helped but note worthy if you are looking to decorate an interface you will want to use the UsedImplicitly attribute
[UsedImplicitly]
public interface ISomeInterface
{
//... stuff
}

Resources