前台頁面代碼:
<!--資料列表開始--> <ScrollViewer HorizontalScrollBarVisibility="Auto" BorderBrush="White" BorderThickness="1" Margin="0,120,0,0"> <StackPanel Height="450"> <!--<Border BorderBrush="Gray" BorderThickness="1">--> <Grid Margin="5,0,0,2"> <Grid.ColumnDefinitions> <ColumnDefinition MinWidth="130" Width="130" MaxWidth="150" /> <ColumnDefinition MinWidth="150" Width="150" MaxWidth="200"/> <ColumnDefinition MinWidth="150" Width="150" MaxWidth="200"/> <ColumnDefinition MinWidth="200" Width="250" MaxWidth="400"/> </Grid.ColumnDefinitions> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="0"> <TextBlock Grid.Column="0" HorizontalAlignment="Center" Text="客戶名稱"></TextBlock> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="1"> <TextBlock Grid.Column="1" HorizontalAlignment="Center" Text="固定電話"></TextBlock> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="2"> <TextBlock Grid.Column="2" HorizontalAlignment="Center" Text="行動電話"></TextBlock> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="3"> <TextBlock Grid.Column="3" HorizontalAlignment="Center" Text="地址"></TextBlock> </Border> </Grid> <!--</Border>--> <ListBox x:Name="listbox1" Height="415" ItemsSource="{Binding}" Margin="0,0,0,0" VerticalAlignment="Top" SelectionChanged="listbox1_SelectionChanged" IsSynchronizedWithCurrentItem="{x:Null}"> <ListBox.ItemTemplate> <DataTemplate> <Grid Height="30" Margin="5,0,0,2"> <Grid.ColumnDefinitions> <ColumnDefinition MinWidth="130" Width="130" MaxWidth="150"/> <ColumnDefinition MinWidth="150" Width="150" MaxWidth="200" /> <ColumnDefinition MinWidth="150" Width="150" MaxWidth="200"/> <ColumnDefinition MinWidth="200" Width="250" MaxWidth="400" /> </Grid.ColumnDefinitions> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="0"> <TextBlock Grid.Column="0" Text="{Binding KHMC}"></TextBlock> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="1"> <HyperlinkButton Grid.Column="1" HorizontalContentAlignment="Left" Tag="{Binding KHMC}" Content="{Binding GDDH}" Click="HyperlinkButton_Click_CALLGDDH"></HyperlinkButton> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="2"> <HyperlinkButton Grid.Column="2" HorizontalContentAlignment="Left" Tag="{Binding KHMC}" Content="{Binding YDDH}" Click="HyperlinkButton_Click_CALLYDDH"></HyperlinkButton> </Border> <Border BorderBrush="Gray" BorderThickness="1" Grid.Column="3"> <TextBlock Grid.Column="3" Text="{Binding DZ}"></TextBlock> </Border> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </ScrollViewer> <!--資料列表結束-->
綁定資料列表:
List<KHModel> lt = new List<KHModel>(); var khlist = from userdata in xele.Descendants("ROW") select userdata; foreach (var khitem in khlist) { KHModel kh = new KHModel(); kh.KHBH = khitem.Element("KHBH").Value; kh.SSGSBH = khitem.Element("SSGSBH").Value; kh.KHMC = khitem.Element("XM").Value; kh.GDDH = khitem.Element("GDDH").Value; kh.YDDH = khitem.Element("YDDH").Value; kh.DZ = khitem.Element("DZ").Value; lt.Add(kh); } listbox1.ItemsSource = lt; //實體類 public class KHModel { public string KHBH { get; set; } public string SSGSBH { get; set; } public string KHMC { get; set; } public string GDDH { get; set; } public string YDDH { get; set; } public string DZ { get; set; } }