使用C#和XAML在metro应用程序中使用标头动态生成gridview

时间:2014-03-20 06:50:59

标签: xaml microsoft-metro

没有固定数量的列。它将动态地来自Web服务。如何以这种方式设计和绑定gridview。

1 个答案:

答案 0 :(得分:0)

尝试以下方式:

Dim sb As New StringBuilder
    sb.Append("<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">")
    sb.Append("<StackPanel Orientation=""Horizontal"">")
    sb.Append("<Grid>")
    sb.Append("<Grid.ColumnDefinitions>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("<ColumnDefinition Width=""auto""></ColumnDefinition>")
    sb.Append("</Grid.ColumnDefinitions>")
    sb.Append("<Grid.RowDefinitions>")
    sb.Append("<RowDefinition Height=""50""></RowDefinition>")
    sb.Append("</Grid.RowDefinitions>")
    sb.Append("<Border Grid.Column=""0""  BorderThickness=""1,0,1,0"" BorderBrush=""White"">")
    sb.Append(" <Button x:Name=""imgEdit"" Grid.Column=""0""  HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Height=""30"" Width=""37"" Click=""EditInventory"" >")
    sb.Append("<Button.Background>")
    sb.Append("<ImageBrush Stretch=""Fill"" ImageSource=""{Binding pencil}""  ></ImageBrush>")
    sb.Append("</Button.Background>")
    sb.Append("</Button>")

    sb.Append("</Border>")

    sb.Append("<Border  Grid.Column=""1""  BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""lblSrNo""   Text=""{Binding serial}""  FontSize=""15"" Grid.Column=""1"" Foreground=""{Binding RForeColor}"" FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center""  Width=""35"" TextAlignment=""Right"" Padding=""1,0,2,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""2""  BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""lblDescription"" Text=""{Binding a3}"" Loaded=""lblDesc_Loaded""  Width=""365"" FontSize=""15"" TextWrapping=""Wrap"" Grid.Column=""2"" Grid.Row=""0"" Foreground=""{Binding RForeColor}""  FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""2,0,1,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""3"" Loaded=""SizeBorderDispaly"" BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""lblSize"" Loaded=""SizeDispaly"" TextWrapping=""Wrap"" Text=""{Binding size}""  Width=""90"" FontSize=""15"" Grid.Column=""3""  Foreground=""{Binding RForeColor}""  FontFamily=""arial"" HorizontalAlignment=""Right"" VerticalAlignment=""Center"" Padding=""2,0,1,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""4"" Loaded=""DepartmentBorderDisplay"" Visibility=""Collapsed"" BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""lblDepartment"" Visibility=""Collapsed"" Loaded=""DepartmentDisplay"" TextWrapping=""Wrap"" Text=""{Binding dept_desc}""  Width=""90"" FontSize=""15"" Grid.Column=""4""  Foreground=""{Binding RForeColor}"" FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""2,0,1,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""5""  BorderThickness=""1,0,1,0"" BorderBrush=""White"">")
    sb.Append("<TextBox x:Name=""txtQty"" TextAlignment=""Right"" Text=""{Binding a5, Mode=TwoWay}"" GotFocus=""txtQty_GotFocus"" LostFocus=""txtQty_LostFocus""  Width=""30""  FontSize=""15"" Grid.Column=""5"" Height=""30""  Foreground=""Black""  FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center""  InputScope=""Number""></TextBox>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""6""  BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""txtPrice"" TextAlignment=""Right"" Text=""{Binding a6, Mode=TwoWay}"" Loaded=""lblDecimalValue_Loaded""  Width=""80""  FontSize=""15"" Grid.Column=""6""  Foreground=""{Binding RForeColor}""   FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""1,0,2,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""7""  BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock x:Name=""lblDisc"" TextAlignment=""Right""  Text=""{Binding a7}"" Width=""70""  FontSize=""15"" Grid.Column=""7"" Loaded=""lblDecimalValue_Loaded""  Foreground=""{Binding RForeColor}""  FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""1,0,2,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""8"" BorderThickness=""1,0,1,0"" BorderBrush=""White"">")
    sb.Append("<TextBlock x:Name=""lblP"" Text=""{Binding Qty}""  Width=""25"" FontSize=""15"" Grid.Column=""8"" Foreground=""{Binding RForeColor}""  FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" TextAlignment=""Center""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""9"" BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock  Width=""100"" TextAlignment=""Right"" Text=""{Binding a11,Mode=TwoWay}"" x:Name=""lblExt"" FontSize=""15"" Loaded=""lblDecimalValue_Loaded"" Grid.Column=""9"" Foreground=""{Binding RForeColor}""   FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""1,0,2,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""10"" Loaded=""lblBorderPts_Loaded"" BorderThickness=""1,0,1,0"" BorderBrush=""White"" >")
    sb.Append("<TextBlock  Width=""70"" TextAlignment=""Right"" x:Name=""lblPts"" Loaded=""lblPts_Loaded"" FontSize=""15"" Grid.Column=""10"" Text=""{Binding points}""  Foreground=""{Binding RForeColor}""  FontFamily=""arial"" HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Padding=""1,0,2,0""></TextBlock>")
    sb.Append("</Border>")
    sb.Append("<Border Grid.Column=""11""  BorderThickness=""1,0,1,0"" BorderBrush=""White"">")
    sb.Append("<Button x:Name=""imgDelete"" Width=""37""  FontSize=""17"" Grid.Column=""11""  HorizontalAlignment=""Center"" VerticalAlignment=""Center"" Height=""30"" Click=""btnDelete_Click_1"">")
    sb.Append("<Button.Background>")

    sb.Append("<ImageBrush Stretch=""Fill"" ImageSource=""{Binding delete}""></ImageBrush>")
    sb.Append("</Button.Background>")
    sb.Append("</Button>")

    sb.Append("</Border>")

    sb.Append("</Grid>")
    sb.Append("</StackPanel>")

    sb.Append("</DataTemplate>")

    Dim datatemplate As New DataTemplate
    datatemplate = TryCast(XamlReader.Load(sb.ToString()), DataTemplate)
    grdCustomer.ItemTemplate = datatemplate