Binding Listbox in Silverlight with DataContext defined from Resource
How to bind a 开发者_StackOverflow社区Listbox in silverlight with DataContext option.I want to have a resource defined in the Usercontrol and want to use it as a static resource List box control.
I'm not sure if I got you well, do you want to have a list box binded to a static resource list of items?, then this would be one way to do it:
I have done it with itemssource not with datacontext, if that's a good approach for you this is what you need to do:
Define the class for each one of the items and another class that will be a list of items
namespace Dashboard.Models
{
public class StringValue
{
#region Properties
public string Value { get; set; }
#endregion
}
public class ValueList : List<StringValue>
{ }
}
In your xaml code in the usercontrol declaration add the reference to the namespace
xmlns:LocalModels="clr-namespace:Dashboard.Models"
Add the static resource to your usercontrol
<Grid.Resources>
<LocalModels:ValueList x:Key="States">
<LocalModels:StringValue Value="--"></LocalModels:StringValue>
<LocalModels:StringValue Value="AL"></LocalModels:StringValue>
<LocalModels:StringValue Value="AK"></LocalModels:StringValue>
<LocalModels:StringValue Value="AZ"></LocalModels:StringValue>
<LocalModels:StringValue Value="AR"></LocalModels:StringValue>
<LocalModels:StringValue Value="CA"></LocalModels:StringValue>
</LocalModels:ValueList>
</Grid.Resources>
Reference your listbox to the static resource
ItemsSource="{StaticResource States}" DisplayMemberPath="Value" SelectedValuePath="Value"
Hope it helps.
精彩评论