1

I have a static class named Building which contains a List<Beam> Beams as its property;

public static class Building
{
    public static readonly List<Beam> Beams = new List<Beam>();
}

public class Beam
{
    public string Story;
    public double Elevation;
}

I'm trying to Bind the Building.Beams to a combobox in XAML so that Elevation and Story properties of each item in Building.Beams list is displayed in different columns in the combobox. I have been able to implement the two columns, I just can't Bind these properties.

Here is what I have tried so far:

<ComboBox x:Name="cmbBuilding"  ItemsSource="{Binding}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="300">
                <TextBlock Width="150"  Text="{Binding Path=Story }"/>
                <TextBlock Width="150" Text="{Binding Path=Elevation}"/>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

var b1 = new Beam { Elevation = 320, Story = "ST1" };
var b2 = new Beam { Elevation = 640, Story = "ST2" };
Building.Beams.Add(b1);
Building.Beams.Add(b2);

3 Answers 3

2

First of all you can't bind with fields.

Convert Story and Elevation to properties (automatic properties in your case will do)

public class Beam
{
    public string Story { get; set;}
    public double Elevation { get; set;}
}

Second, you should use ObservableCollection in case you are adding items to the list after loading finishes so that UI gets notification.

public static readonly ObservableCollection<Beam> Beams
                                   = new ObservableCollection<Beam>();
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Thank you. I've been working on this for nearly 3 hours now!
1

Try this example:

XAML

<Grid>
    <ComboBox x:Name="cmbBuilding" Width="100" Height="25" ItemsSource="{Binding Path=Beams}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="300">
                    <TextBlock Width="150" Text="{Binding Path=Story}" HorizontalAlignment="Left" />
                    <TextBlock Width="150" Text="{Binding Path=Elevation}" HorizontalAlignment="Right" />
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <Button Content="Add item" VerticalAlignment="Top" Click="Button_Click" />
</Grid>

Code-behind

public partial class MainWindow : Window
{
    Building building = new Building();

    public MainWindow()
    {
        InitializeComponent();

        building.Beams = new List<Beam>();

        building.Beams.Add(new Beam 
                           { 
                               Elevation = 320, 
                               Story = "ST1" 
                           });

        this.DataContext = building;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var b1 = new Beam { Elevation = 320, Story = "ST1" };
        var b2 = new Beam { Elevation = 640, Story = "ST2" };

        building.Beams.Add(b1);
        building.Beams.Add(b2);

        cmbBuilding.Items.Refresh();
    }
}

public class Building
{
    public List<Beam> Beams
    {
        get;
        set;
    }
}

public class Beam
{
    public string Story 
    {
        get;
        set;
    }

    public double Elevation
    {
        get;
        set;
    }
}

Some notes

  • When you use properties in the Binding, you need to be properties with get and set, not fields.

  • Properties, what were added to the List<T> will automatically update, you should call MyComboBox.Items.Refresh() method, or use ObservableCollection<T>:

ObservableCollection represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

10 Comments

Thank you so much Anatoliy for the time you put into this. I'm always learning from guys like you.
Just one thing, my Building class is static. So it wouldn't let me to assign it as a DataContext. So I had to do this: DataContext = Building.Beams Is there a better way?
@Vahid: Why do you use static here? Binding usually not used static classes, I advise to do as my example. For Binding in static class, implementing pattern Singleton.
I've heard that Singleton pattern must be used in very rare situations. Actually I have built my whole program around this static Building class so I have to stick to it.
I've heard that Singleton pattern must be used in very rare situations. - I think this is your case. Or are you doing in my example, or to implement a Singleton pattern. You can create a new question about this problem, if you want.
|
1

Is it maybe because you have declared Beams as readonly yet you try to ADD items to it? Beams is also defined as a variable, try removing the readonly and making it a property with a getter and setter

1 Comment

Yes, it was because of the fields I had used instead of the properties. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.