嘿伙计我有以下问题,
我正在尝试将变量绑定到列表框以显示字符串列表。当新项目添加到列表时,这应该会自动更改,因此我使用了ObservableCollection。但是我似乎无法将变量绑定到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>
所以我正在尝试将Chat属性从Group绑定到此Listbox。
答案 0 :(得分:2)
如果您将DataContext
的{{1}}设置为Page
类的实例:
Group
...你应该能够绑定它的任何属性:
public GroupPage(GroupController groupController)
{
InitializeComponent();
this.groupController = groupController;
LoadData();
DataContext = group;
}