StringFormat in silverlight Xaml and resources - xaml

I have format strings in my resource files. I am trying to access these from the Text attribute of TextBlock using FormatString
Text="{Binding Path=Project.Name, StringFormat={Binding Path=WkStrings.DisplayProjectName, Source={StaticResource ResourceWrapper}}}"
I am getting the following error:
Provide value on 'System.Windows.Data.Binding' threw an exception
Error points to Text=.
Is it possible to access resources from a "nested binding"?

Binding.StringFormat is not a dependency property and therefore you cannot set a binding to this property. If you want to assign a value to that property, your value has to be a static resource, like this:
<TextBlock Text="{Binding ProjectName, StringFormat={StaticResource ProjectNameFormat}}"/>
You should declare your resource like this:
<UserControl.Resources>
<System:String x:Key="ProjectNameFormat">Project: {0}</System:String>
</UserControl.Resources>
The end result looks like this:

Your syntax is wrong for using StringFormat and you may want something other than StringFormat. StringFormat is used to manipulate the output of what is assigned to the Path of the Binding. In your example you're binding to the Project.Name property.
StringFormat should be used to achieve the similar effect as using String.Format in code. See this reference for formatting: http://msdn.microsoft.com/en-us/library/26etazsy(v=VS.95).aspx
Other answers around this topic:
Does Silverlight support StringFormat in binding?
http://blog.davemdavis.net/2009/12/03/silverlight-4-data-binding-string-format/
Here's some example code of using StringFormat:
<TextBlock Text="{Binding Path=Cost, StringFormat=\{0:c\}}" />

Related

How to format label in Xamarin XAML Listview based on a viewmodel attribute

I am trying to create a ListView cell data template with a label that shows either a "detailed" description or a "short" description based on whether another variable ViewLevel is "Detailed" or "Short".
I am using an IValueConverter and trying to bind the ConverterParameter, but that does not work as I don't think the ConverterParameter is bindable.
Is there a best practice for doing this?
The label I currently have is:
<Label Binding Options, Converter={viewModels:DetailLabelConverter}, ConverterParameter='Detail'}" />
It works, but obviously has the hardcoded Detail view. I've also tried:
<Label Text="{Binding Options, Converter={viewModels:DetailLabelConverter}, ConverterParameter={Binding Source={x:Reference BasePage}, Path=BindingContext.ViewLevel}}"/>
Which works, insomuch as it calls the DetailLabelConverter. Unfortunately the parameter is not loaded with the value of ViewLevel but a Xamarin.Forms.Binding object.
It seems like this should be a fairly common pattern but I can't find a reasonable solution.

C# Windows Store App - Find resource on xaml

If i have 9 TextBlock declared on the XAML file like this:
<TextBlock Name="cellText_00" Tag="0"/>
<TextBlock Name="cellText_01" Tag="1"/>
<TextBlock Name="cellText_02" Tag="2"/>
<TextBlock Name="cellText_20" Tag="3"/>
...
<TextBlock Name="cellText_22" Tag="8"/>
There is a way to interact with it from the .cs getting exactly the desired tag element?
For instance is it possible to give all the same name and get it in this way:
TextBlock tb = get(cellText,0);
where the first field is the name and the second one is the tag?
No, you can't use the same name for many controls.
However there is a workaround: using the FindName method:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname(v=vs.95).aspx
There Why can't I access a TextBox by Name with FindName()?
you can find an example and a solution related to namespaces issues.
FindName uses a string to retrieve the control. So you can do something like this: FindName("cellText_" + identifier); and take the element you need.
#Sandrouos, I don't think he's using the same name.
This blogpost explains it perfectly:
http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

How to data bind based on emptiness of a collection?

I want to make a TextBlock visible only when a collection is empty. I have a ListView that is data-bound to the collection already and that's simple. I just want to display something else when the ListView is otherwise empty.
I wrote an IValueConverter that would take the collection, or count, or whatever I need, and return a Visibility appropriately. The XAML looks like this:
<TextBlock Visibility="{Binding Count, ElementName=ContactsList, Converter={StaticResource visibilityWhenEmpty}}"
Text="No contacts yet. Add one using the AppBar below." />
The trouble is that binding just the collection itself only calls my value converter once, when it's empty, and not again when the contents of the collection changes (kinda makes sense). And when, as shown above, I try binding against the collection's Count property, it doesn't call my value converter at all.
Any ideas?
I have faced the same issue. I applied a tricky solution. It might work for you as well. Taks a temporary combo box and bind the collection with that.
<ComboBox x:Name="TempComboBox"
ItemsSource="{Binding DataContext.ContactsList,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" >
<ComboBox.Template>
<ControlTemplate>
<!--Add file button..-->
<TextBlock Content="Your text..."
>
<TextBlock.Visibility>
<Binding Path="Items.Count"
RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ComboBox}"
Converter="{StaticResource visibilityWhenEmpty}">
</Binding>
</TextBlock.Visibility>
</TextBlock>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
You could use notifyPropertyChanged for Count, or implement using BindableBase.

Declare a Nullable int (int?) using XAML

I am trying to bind a combo box to a property on my ViewModel. The target type is short? and I would like to have null be an option. Basically I would like the value of the first item in the combo box be {x:Null}.
<ComboBox Grid.Row="9" Grid.Column="1" SelectedValue="{Binding Priority}">
<clr:Int16></clr:Int16>
<clr:Int16>1</clr:Int16>
<clr:Int16>2</clr:Int16>
<clr:Int16>3</clr:Int16>
<clr:Int16>4</clr:Int16>
<clr:Int16>5</clr:Int16>
<clr:Int16>6</clr:Int16>
<clr:Int16>7</clr:Int16>
<clr:Int16>8</clr:Int16>
<clr:Int16>9</clr:Int16>
<clr:Int16>10</clr:Int16>
</ComboBox>
Any Suggestions?
If you are using XAML 2009 / .NET 4 then you can use a new syntax for creating generics using XAML.
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
<Nullable x:TypeArguments="clr:Int16" />
This article has other, more complex, scenerios for generics in XAML.

XPath syntax within binding XAML

What is the syntax for using XPath with Binding in XAML? Are there any MSDN pages which describe where to put the braces?
Visual Studio doesn't like the following:
<TextBlock Text="{Binding XPath=/One/Two[#id='0']/Three/#Four}" />
I want the Text of the TextBlock to be set to the value of the Four attribute.
Looking at the documentation, you should set the binding using nested syntax as follows:
<TextBlock>
<TextBlock.Text>
<Binding XPath="/One/Two[#id='0']/Three/#Four" />
</TextBlock.Text>
</TextBlock>