Hello and thanks in advance for the assistance. I am having an issue using databinding with a List of objects who have a property I want shown in a listbox.
I have a a list box in my Silverlight child window whose xaml looks like this:
<StackPanel Orientation="Vertical" Width="235"> <sdk:Label Content="Servers" HorizontalAlignment="Center"></sdk:Label> <!--new group servers list box--> <ListBox x:Name="NewGroupServersLB" Height="150" Width="200" ItemsSource="{Binding}" SelectionChanged="NewGroupServersLB_SelectionChanged" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=ServerName}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel>
I am setting its DataContext in the code behind using a Binding object instance with this code:
/*create new binding source object */ Binding serversBinding = new Binding(); /*set data source */ serversBinding.Source = childServersList; /*set the data source for the new group servers listbox*/ NewGroupServersLB.DataContext = serversBinding;
I can see using debugging that the Binding object's source has two members, and that they are the appropriate server object instances from my childServersList. However, nothing is in the Listbox when I run it. However, when I set the listbox's datacontext directly to the servers list in the code behind, the server names show up in the listbox. Could you please tell me why that is or if I am doing something wrong? The reason I was attempting to use a Binding object is because I want to populate another listbox with children members of the selected server object from the first listbox. Thank you in advance for the help, and I apologize for the verbosity of this question.
In case it is helpful, my server object that is stored in the list looks like this:
[XmlRoot("Server")] public class RegisterServerObject { public RegisterServerObject() { } [XmlElement("ServerID")] [Browsable(false)]//not displayed in grids [EditorBrowsable(EditorBrowsableState.Never)]//not displayed by intellisense public string ServerIDString { set { ServerID = Convert.ToInt32(value);//convert the value passed in by xml to your type } get { return Convert.ToString(ServerID); } } [XmlIgnore] public int ServerID { get; set; } [XmlElement("GroupID")] public string GroupIDString { get { return this.GroupID.ToString(); } set { if (string.IsNullOrEmpty(value)) { this.GroupID = 0; } else { this.GroupID = int.Parse(value); } } } [XmlIgnore] public int GroupID { get; set; } [XmlElement("ParentID")] public int ParentID { get; set; } [XmlElement("ServerName")] public string ServerName { get; set; } [XmlElement("User")] public string User { get; set; } [XmlElement("UID")] public int Uid { get; set; } [XmlElement("PWD")] public string Domain { get; set; } [XmlElement("Location")] public string Location { get; set; } [XmlArray(ElementName = "AssociatedModules")] [XmlArrayItem(ElementName = "Module")] public List<RegisterModuleObject> AssociatedModules { get; set; }
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog