0

I have a small problem, suppose I have the following MainWindow.xaml:

<Window x:Class="DragDrop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Name="ListBoxLeft"  />
        <ListBox Name="ListBoxRight"  Grid.Column="1" />
    </Grid>
</Window>

and now I would simply like bind <ListBox Name="ListBoxLeft" /> to my property public List<User> UserListLeft;. How can I achieve this? How should I specify the DataContext?

All properties are just simply listed in MainWindow.xaml.cs.

Note: I do not use the MVVM model.

Thanks

1 Answer 1

2

In the constructor of your MainWindow.xaml.cs add:

DataContext=this;

then in your MainWindow.xaml, add:

 <ListBox Name="ListBoxRight"  Grid.Column="1" ItemSource={Binding UserListLeft} />

Please make your UserListLeft as an ObservableCollection, instead of List. To be able to notify any change to the view.

Sign up to request clarification or add additional context in comments.

4 Comments

Dear Alvaro, thank you for your kind response. I was wondering whether it was also possible to do the DataContext=this; part in XAML.
@Snowflake So what you want to do can't be called a "Binding". In this case, I suggest for you to fill it Manually, in the "Set method" of your UserListLeft property. each time you add an "Item" to your list, add also : ListBoxRight.Items.add(YourItem);
In XAML top / Window DataContext="{Binding RelativeSource={RelativeSource self}}"
Also, you should have the mainWindow implement INotifyPropertyChanged

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.