Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Binding values from a dictionary
6 replies. Latest Post by slyi on August 17, 2009.
(0)
Odegaard
Member
35 points
48 Posts
11-21-2008 6:32 PM |
I have a Dictionary<string,object> assigned to the datacontext, and want to bind the values in XAML. In WPF I would do like this:
<TextBlock Text="{ Binding Path=.[myKey] }" />
but this doesn't work in Silverlight. I get:Error: Sys.InvalidOperationException: Invalid XAML for control 'Xaml1'. Invalid attribute value {Binding Path=.[ID]} for property Text.
What is my alternative in Silverlight? I would really like to have code that is identical in Silverlight and WPF. I can live with the XAML being different, but I don't want to have two different code implementations as well (in other words I would really like to stick to using a Dictionary).
bryant
Star
9937 points
1,629 Posts
11-21-2008 8:01 PM |
It looks like this really isn't supported.
Here is how you can do this using a type converter:
public class DictionaryItemConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var dict = value as Dictionary<string, string>; if (dict != null) { return dict[parameter as string]; } throw new NotImplementedException(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Then your binding should look like:
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <local:DictionaryItemConverter x:Name="DictConvert" /> </Grid.Resources> <TextBlock Text="{Binding Converter={StaticResource DictConvert}, ConverterParameter=test}" /> </Grid>
And you can then pass a dictionary and it should work:
Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("test","Testing 123"); dict.DataContext = data;
ken tucker
All-Star
16276 points
2,479 Posts
11-21-2008 9:23 PM |
I was thinking more along these lines. Keep in mind you would have to bind to a list control to see the values not the properties of the key value collection
the xaml
<ListBox x:Name="listBox1" Height="200"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
The code
Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Dim ht As New Dictionary(Of String, String) For x As Integer = 0 To 20 Dim s = String.Format("Item {0}", x) ht.Add(s, s) Next listBox1.ItemsSource = ht.Values End Sub
11-22-2008 2:32 PM |
Thanks, I know I can do it with a type converter, but this is for an API SDK sample/reference, and this solution is going to scare people away. I was hoping for a simple XAML only method.
What about another type than Dictionary that holds string keys and object values? I was trying to go with a anonymous type which does work, but since I don't know the keys until runtime, I would need to look into dynamic languages which I would rather avoid..
I'm getting more and more dissappointed about the differences and inconsistencies between Silverlight and WPF (I'm not just talking about the things they left out). I've come to the conclusion that Silverlight is NOT a subset of WPF. It's a total rewrite from grounds up made to look and feel like WPF, but they missed a LOT along the way. For instance something as basic as x:Key and x:Name are completely messed up in the two frameworks and behaves differently, and something as simple as these binding scenarios just lacks way too much (the binding is/should have been the cool part).
silverbyte
Participant
1338 points
405 Posts
11-22-2008 6:03 PM |
I agree to your point Odegaard.
I wonder where Silverlight goes. They want to make it to look like WPF.
In the same time, Silverlight can't be WPF obviously but they have to put in Silverlight at least the basic features from WPF otherwise it stays crippled. People with experience in WPF face frustration very soon after starting to work in Silveright.
11-23-2008 3:09 AM |
Actually I'm trying to get Silverlight code to run on WPF, which should be a straight forward task, especially now with the VisualStateManager for WPF. So I'm only talking about the subset part - Of course I didn't expect there to be the same amount of functionality, but the stuff that is the same, should be the same! I'm amazed at the amount of compiler conditionals I have to put in to actually get this to work. I wish we had compiler conditionals for XAML as well :-)
At the PDC Microsoft several times talked about how easy this now is to share code, but I don't think they really tried it on anything but a very simple application. You don't have to dig very deep before you hit some serious incompatibilities. The session that specifically dealt with this only scratched the surface (all he did was/copy paste a couple lines of XAML from Silverlight to WPF and didn't look at code at all)
The worst part is that fixing some of these issues would cause breaking changes in either Silverlight or WPF. So far the only upside I've had with my code running on both platforms is that since the Silverlight debugger is HORRIBLE at giving helpful error messages, running it as WPF usually gives an error message that is right on the issue.
slyi
824 points
254 Posts
08-17-2009 3:43 AM |
Using dictionary to strongly-typed object reflection, you can get the following type syntax to work.
<UserControl.Resources>
....
<
code: http://cid-289eaf995528b9fd.skydrive.live.com/self.aspx/Public/MDic.zip