I have written a service
[OperationContract]
Dictionary<string, string> GetItemNames(UInt16 mobileNO);
I have written a simple windows mobile 7 client which needs to consume the data returned by the wcf service.
开发者_如何学CBeing a beginner I am unable to understand how to do this. I have tried using simple data like string, int or bool.
How do I consume a complex type like dictionary or custom object?
Binding to objects is no different that binding to primitive types. Since dictionaries are IEnumerable you can bind it to the ItemsSource property of any Items Control and the set the DisplayMemberPath=”Value”.
public MainPage()
{
InitializeComponent();
Dictionary<int, string> dic = new Dictionary<int, string>();
for (int i = 1; i < 11; i++)
{
dic.Add(i, string.Format("Item {0}", i));
}
lstBox.ItemsSource = dic;
}
<ListBox x:Name="lstBox"
DisplayMemberPath="Value"
Margin="5" />
For objects the following binding is valid:
<TextBlock Text="{Binding Object.Property}" />
If you are using MVVM then you might want to break out the properties of your model object in the ViewModel and bind specifically to that.
In ListBox
DataTemplate
you can bind with Key
and Value
statements to a Dictionary
<StackPanel Orientation="Horizontal" Margin="12,0">
<TextBlock Text="{Binding Key}" />
<TextBlock Text=": " />
<TextBlock Text="{Binding Value}" />
</StackPanel>
精彩评论