• 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
手在键盘敲很轻 程序员  2020年12月18日
WPF DataGrid 数据绑定问题


<Window x:Class="CSTK.Library.DownLoad.MainWindow"
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:CSTK.Library.DownLoad"
mc:Ignorable="d"
Title="下载中心" Height="542.406" Width="1016.225" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" MaxHeight="532.406" MaxWidth="1000" >
<Grid>
<Grid Margin="10,2,0,451" Background="Transparent" Height="50">
<TextBox BorderBrush="Black" Name="search_Word" FontSize="14" FontFamily="微软雅黑" Text="林" TextAlignment="Center" Width="100" Height="32" Padding="5" Margin="-50 0 0 8"/>
<Button BorderBrush="Black" FontSize="14" FontFamily="微软雅黑" Name="search" Margin="0,8,325,0" RenderTransformOrigin="0.211,-7.49" Click="Button_Click" HorizontalAlignment="Right" Width="89" Height="30" VerticalAlignment="Top">搜索</Button>
</Grid>
<Grid>
<DataGrid Margin="10,63,10,9" AutoGenerateColumns="True" Name="dg_Table" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="歌曲名" Binding="{Binding Path=SongName}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="歌手" Binding="{Binding Path=SingerName}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="专辑名" Binding="{Binding Path=AlbumName}" Width="*"></DataGridTextColumn>
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Button Content="下载"></Button>
</StackPanel>
</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>

</Grid>
</Window>


后台代码

public class SongDetail {
public string ID { get; set; }
public string SongName { get; set; }
public string AlbumName { get; set; }
public string SingerName { get; set; }
}
System.Threading.Thread th = new System.Threading.Thread(() =>
{
var rst = HttpHelper.HttpPost(url, data, HttpHelper.cookie, "application/x-www-form-urlencoded; charset=UTF-8");
var root = JsonToEntity<Root>(rst);
ObservableCollection<SongDetail> songItems = new ObservableCollection<SongDetail>();
foreach (var song in root.song_list)
{
string name = song.songname;
var arr = song.songname.Split(new string[] { "<sup" }, StringSplitOptions.None);
if (arr != null && arr.Length > 1) name = arr[0];
song.songname = name;
var singer = song.singer.FirstOrDefault();
songItems.Add(new SongDetail() {ID=song.songmid,SongName=name,AlbumName=song.albumname,SingerName=singer==null?"":singer.name });
}
dg_Table.Dispatcher.Invoke(delegate() {
dg_Table.ItemsSource = songItems;
// dg_Table.GridLinesVisibility = DataGridGridLinesVisibility.All;
});
});
th.IsBackground = true;
th.Start();

看图,我声明的列有值了,这个正常,但是出现属性的列是什么情况,百度了半天没找到原因。
代码大概就这些了,最近在学习WPF,可能在将来工作中用的上。望各位大神帮忙!
...全文
7139 点赞 收藏 5
写回复
5 条回复

还没有回复,快来抢沙发~

发动态
发帖子
.NET技术社区
创建于2007-09-28

4.8w+

社区成员

66.8w+

社区内容

.NET技术交流专区
社区公告
暂无公告