范例示范使用RowDetailTemplate
图
13.1-6
是
Ch13_Exam1_4
的运行画面,单击某一行后,出现详细信息。
图
13.1-6
◎
XAML
标记如下,已添加注解。
<UserControl
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:myheader="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="SilverlightApplication74.MainPage"
d:DesignWidth="640" d:DesignHeight="580"
xmlns:local="clr-namespace:SilverlightApplication74">
<Grid x:Name="LayoutRoot">
<StackPanel Margin="10,0,8,18">
<TextBlock Height="51" Margin="179,0,299,0" Text="热门新车销量表" TextWrapping="Wrap" FontSize="32" Foreground="#FFE90D0D"/>
// 设置RowDetailsVisibilityMode属性
<data:DataGrid x:Name="gridStudent" Margin="10,10,0,0" AutoGenerateColumns="False" Width="604" Height="488" HorizontalAlignment="Left" RowDetailsVisibilityMode="VisibleWhenSelected" >
//定义详细数据区域
<data:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border>
<Border Margin="10" Padding="10" BorderBrush="SteelBlue" BorderThickness="4" CornerRadius="4">
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" FontSize="14"></TextBlock>
</Border>
</Border>
</DataTemplate>
</data:DataGrid.RowDetailsTemplate>
// 定义ColumnHeaderStyle列标题样式
<data:DataGrid.ColumnHeaderStyle>
<Style TargetType="myheader:DataGridColumnHeader">
<Setter Property="FontSize" Value="18" />
<Setter Property="Foreground" Value="Red" />
</Style>
</data:DataGrid.ColumnHeaderStyle>
<data:DataGrid.Columns >
<data:DataGridTextColumn Header="车名"
FontSize="16"
Width="100"
Binding="{Binding Name}" >
</data:DataGridTextColumn>
<data:DataGridTextColumn Header="类型"
FontSize="16"
Width="100"
Binding="{Binding Type}" >
</data:DataGridTextColumn>
<data:DataGridTextColumn Header="销量"
FontSize="16"
Width="150"
Binding="{Binding Total,Mode=TwoWay}">
<data:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</data:DataGridTextColumn.ElementStyle>
<data:DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Red" />
</Style>
</data:DataGridTextColumn.EditingElementStyle>
</data:DataGridTextColumn>
<data:DataGridTemplateColumn Header="图片" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Stretch="UniformToFill" Source ="{Binding Img}" Width="150"/>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Car, Mode=TwoWay}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
</StackPanel>
</Grid>
</UserControl>
◎
主要代码如下:
public void LoadData()
{
car = new Car[] {
new Car{
Name="新明锐",
Total =8500,
Type="小型车",
Img=GetPath("CarPhoto/mingru.jpg"),
Description="斯柯达品牌秉承德国大众集团的先进技术,其产品特点是智慧和品质…………….",
},
new Car{
Name="高尔夫6",
Total =15500,
Type="豪华车",
Img=GetPath("CarPhoto/golf.jpg"),
Description="高尔夫不仅追求外观和实用性完美结合,而且在产品品质、内饰及舒适性装备上也毫不妥协地追求完美…………….."
},
new Car{
Name="科鲁兹",
Total =11000,
Type="中型车",
Img=GetPath("CarPhoto/kouluz.jpg"),
Description="科鲁兹的外形给人一种锋利运动的感觉,尤其是锐角的前大灯更强化了……………."
},
new Car{
Name="福克斯两厢",
Total =12800,
Type="紧凑型车",
Img=GetPath("CarPhoto/fukes.jpg"),
Description="福克斯两厢车头采用的X焦点设计,正是福特汽车车头设计的进化成果……………."
},
new Car{
Name="速腾1.4T",Total =10600,
Type="中型车",
Img=GetPath("CarPhoto/suteng.jpg"),
Description="速腾是一汽大众于2006年4月9日投放中国市场的一款新车型,其英文名称为…………….",
}
};
}
//读取图片的绝对路径
string GetPath(string path)
{
string Uri;
string absoUri= System.Windows.Application.Current.Host.Source.AbsoluteUri;
if (absoUri.IndexOf("ClientBin") > 0)
Uri=absoUri.Substring(0, absoUri.IndexOf("ClientBin")) + path;
else
Uri=absoUri.Substring(0, absoUri.LastIndexOf("/") + 1) + path;
return Uri;
}
}
//定义汽车类
public class Car
{
public string Name
{ set;get;}
public int Total
{ set;get;}
public string Type
{ set;get; }
public string Description
{ set;get; }
public string Img
{ set;get;}
}
更详细内容及源代码下载:
http://www.amazon.cn/mn/detailApp/ref=sr_1_1?_encoding=UTF8&s=books&qid=1287058088&asin=B0043RT7I2&sr=8-1