Christian
Christian

Reputation: 1735

Correct use of databinding with a listbox in with Binding Object

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; }

Upvotes: 0

Views: 452

Answers (1)

Gambit
Gambit

Reputation: 2525

There are a couple of different ways to specify the binding source (the object you are data binding to). The first is to specify the DataContext. This can be done in XAML or via code.

NewGroupServersLB.DataContext = childServersList;

Note that the DataContext is set to the source object.

If you are using a Binding object instead, you would set the Source of the Binding object to be your binding source.

MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

In this case you are setting up the Binding object, and then applying that to the control. DataContext is not set, but if it was, would be ignored since the Binding object will take precedence.

For your particular example, you may want to read this particular section of the Data Binding documentation:

http://msdn.microsoft.com/en-us/library/ms752347.aspx#master_detail_scenario

Upvotes: 1

Related Questions