【发布时间】:2013-06-21 13:52:04
【问题描述】:
您好,我正在开发 WPF 聊天应用程序,我想将用户图片保存在数据库中,并且我想将每个用户及其图片加载到朋友列表框中,这是我当前的代码,但我没有知道如何从这个开始。
private void LoadFriends(Client.State client)
{
foreach (Client.Structures.Society.Friend friend in client.Friends.Values)
{
Friends_listBox.Items.Add(friend.Name);
FriendsCount++;
}
}
我知道如何使用这样的 Xaml 代码将项目添加到列表框中:
<ListBoxItem Background="LightCoral" Foreground="Red"
FontFamily="Verdana" FontSize="12" FontWeight="Bold">
<StackPanel Orientation="Horizontal">
<Image Source="/my_App;component/Img.ico" Height="30"></Image>
<TextBlock Text="Coffie"></TextBlock>
</StackPanel>
</ListBoxItem>
但我不知道用 C# 代码来做这个...
我如何将用户图片存储到数据库??
对此有什么想法吗?
【问题讨论】:
-
创建一个包含 2 个属性(
string名称,ImageSource图像)的类(我们称之为Friend)并使其实现 INPC。现在在您的代码隐藏中创建一个ObservableCollection<Friend> FriendsList并将您的ListBox的ItemSource设置为FriendsList。在 xaml 中,将ListBox.ItemTemplate设置为DataTemplate用于Type="{x:Type local:Friend}"。在此 DataTemplate 中,现在有您的StackPanel和 ChildImage,其Source可以从 DataContext 绑定到Image和类似的TextBlock->Name。还将 xaml 视图的 DataContext 设置为包含FriendsList的代码隐藏 -
谢谢,我已经创建了一个像这样
public class Friend : Interfaces.IKnownPerson { public string Name { get; set; } public System.Windows.Media.ImageSource FriendAvatar { get; set; } }的名为Friend 的类,但不知道下一步如何加载图像...