0

Hey guys I have the following issue,

I'm trying to bind a variable to a listbox to show a list of strings. This should change automatically when a new item is added to the list so I used an ObservableCollection. However I can't seem to bind the variable to the Listbox.

GroupPage.xaml.cs:

private readonly GroupController groupController;
private Group group;

public GroupPage(GroupController groupController)
{
    InitializeComponent();
    this.groupController = groupController;
    LoadData();
}

private void LoadData()
{
    group = groupController.GetGroup();
    LblGroupName.Content = group.Name;
}

private void BtnDetails_OnClick(object sender, RoutedEventArgs e)
{
    throw new NotImplementedException();
}

private void btnSend_Click(object sender, RoutedEventArgs e)
{
    string message = "[Chat] " + group.GroupId + " " + DataLayer.Instance.CurrentLoggedInUser.FirstName + " " + tbChat.Text;
    MainController.Instance.SendToServer(message);
}

Group.cs:

public int GroupId { get; set; }

public string Name { get; set; }

public string InterestId { get; set; }

public DateTime Creation { get; set; }

public string Description { get; set; }

public ObservableCollection<string> Chat { get; set; } = new ObservableCollection<string>();

public Group()
{

}

public Group(string name, string description, string interestId)
{
    Name = name;
    Description = description;
    InterestId = interestId;
}

public Group(MySqlDataReader reader)
{
    GroupId = reader.GetInt32("group_id");
    Name = reader.GetString("group_name");
    Description = reader.GetString("group_description");
    Creation = reader.GetDateTime("creation_date");
}

GroupPage.xaml

<Page x:Class="TeamRockstarPlatform.View.GroupPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TeamRockstarPlatform.View"
    mc:Ignorable="d" d:DesignHeight="1080" d:DesignWidth="1920"
    Title="Group Page">
<Grid>
    <Rectangle Fill="#FFFFE000" Height="86" VerticalAlignment="Top" Width="Auto" HorizontalAlignment="Stretch"/>
    <Rectangle Fill="#FF232323" Height="86" VerticalAlignment="Top" Width="28" HorizontalAlignment="Left"/>
    <Ellipse x:Name="ImgGroup" Fill="White" HorizontalAlignment="Left" Height="125" Margin="125,24,0,0" Stroke="#FF232323" VerticalAlignment="Top" Width="125" StrokeThickness="3"/>
    <Label x:Name="LblGroupName" Content="Management" HorizontalAlignment="Left" Margin="255,51,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Microsoft Sans Serif"/>

    <Button x:Name="BtnDetails" Content="Details" Click="BtnDetails_OnClick" HorizontalAlignment="Right" Margin="0,102,1563,0" VerticalAlignment="Top" Width="102" Background="#FFFFE000" BorderBrush="{x:Null}" Foreground="White" FontFamily="Microsoft Sans Serif" FontSize="20" Height="35"/>


    <Grid x:Name="Chat" Margin="0,150,0,0">


    </Grid>
    <Grid x:Name="Details" Margin="0,150,0,0">
        <Grid>
            <!-- BASIC INFO -->
            <TextBox x:Name="TbxGroupName" MaxLength="50" HorizontalAlignment="Left" Height="35" Margin="257,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="519" FontFamily="Microsoft Sans Serif" FontSize="22" BorderBrush="Black"/>
            <RichTextBox x:Name="TbxDescription" HorizontalAlignment="Left" Height="146" Margin="257,71,0,0" VerticalAlignment="Top" Width="519" FontFamily="Microsoft Sans Serif" FontSize="22" BorderBrush="Black"/>
            <Button x:Name="BtnSave" Content="Save" HorizontalAlignment="Left" Margin="257,244,0,0" VerticalAlignment="Top" Width="103" Background="#FFFFE000" BorderBrush="{x:Null}" Foreground="White" FontFamily="Microsoft Sans Serif" FontSize="20" Height="35"/>
            <Label Content="Name: " HorizontalAlignment="Left" Margin="145,20,0,0" VerticalAlignment="Top" FontFamily="Microsoft Sans Serif" FontSize="22"/>
            <Label Content="Description: " HorizontalAlignment="Left" Margin="94,71,0,0" VerticalAlignment="Top" FontFamily="Microsoft Sans Serif" FontSize="22"/>

            <Button x:Name="btnSend" Content="Send" HorizontalAlignment="Left" VerticalAlignment="Top" Width="287" Margin="257,508,0,0" Height="38" Click="btnSend_Click"/>
            <TextBox x:Name="tbChat" HorizontalAlignment="Left" Height="23" Margin="257,480,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="287"/>
            <ListBox HorizontalAlignment="Left" Height="171" Margin="257,304,0,0" VerticalAlignment="Top" Width="287"/>
        </Grid>
    </Grid>

</Grid>

So I'm trying to bind the Chat property from Group to this Listbox.

1
  • Further to mm8's answer, once group is your DataContext, you should be binding LblGroupName.Content as well: <Label Content="{Binding Name, FallbackValue=Management}" />, rather than assigning it in codebehind. Commented May 16, 2018 at 14:09

1 Answer 1

3

If you set the DataContext of the Page to an instance of your Group class:

public GroupPage(GroupController groupController)
{
    InitializeComponent();
    this.groupController = groupController;
    LoadData();
    DataContext = group;
}

...you should be able to bind to any property of it:

<ListBox ... ItemsSource="{Binding Chat}" />
Sign up to request clarification or add additional context in comments.

Comments

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.