I have a listbox which is bound to a list of Currencies items. Currency is a class
public class Currency
{
public string code { get; set; }
public string countryName { get; set; }
public string imgUrl { get; set; }
public string infoLink { get; set;} }
}
A list box is bound to a list of Currencies Objects and each item in this listbox is a stackpanel of an image and textblock
I want to bind the SelectedItem property to a property in the Code-behind to keep up
<ListBox Name="sCurrencyLB" Margin="10,0,0,0" Width="Auto" Height="180"
IsEnabled="{Binding IsChecked, ElementName=LiveTilesToggleBtn}"
SelectedItem="{Binding STileCurrency, Mode=TwoWay,
Source={StaticResource livetilemanager}}"
ItemsSource="{Binding SCurrencyList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Name="scountryNametb" Width="50" Text="{Binding code}"
VerticalAlignment="Center" HorizontalAlignment="Right"/>
<Image Source="{Binding imgUrl}" Height="50" Width="50"
HorizontalAlignment="Left" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code of the property that should beind the selected item in the listbox
private Currency sTileCurrency;
public Currency STileCurrency
{
get
{
return appSettings.GetValueorDefault<Currency>("STileCurrency", null);
}
set
{
if (appSettings.AddOrUpdateValue("STileCurrency", value))
{
settings.Save();
}
}
}
Note : I created an instance of Class containing the property inside XAML