如何在行和列XAML窗口8中显示数据

时间:2012-06-03 10:54:29

标签: xaml windows-8

在metro风格的Windows 8应用程序中,如何使用类似于此的(https://dl.dropbox.com/u/59251888/img.png)图像的xaml显示数据。可以使用ListBox,ListView,GrdView ..?

1 个答案:

答案 0 :(得分:5)

是的,但您需要对其进行样式设置,使其看起来像数据网格。 (假设您正在基于您已分配给此问题的标记在XAML中进行开发)。诀窍是制作一个使用Grid的数据模板,其中列具有适当的宽度,对齐等。

我做了类似的事情 - 使用ListView。这可以修改为使背景只出现在单元格而不是行 - 我已经完成了。希望这会有所帮助:

ListView simulating the look of a data grid / table

XAML:

 <ListView 
      VerticalAlignment="Top"
      Margin="0,5"
      ItemsSource="{Binding HighestExpensesAlternatingList}"
      ItemTemplate="{StaticResource HighestExpensesTemplate}"
      BorderBrush="#19FFFFFF" BorderThickness="1,0,0,0" 
      SelectionMode="None" IsItemClickEnabled="False"
      ScrollViewer.VerticalScrollBarVisibility="Hidden"
      ScrollViewer.HorizontalScrollBarVisibility="Hidden"
      ItemContainerStyle="{StaticResource SimpleListViewItemStyle}"
      IsHitTestVisible="False"/>


    <DataTemplate x:Key="HighestExpensesTemplate">
        <Grid Width="500" VerticalAlignment="Center" Margin="5,0"
              Background="{Binding AlternatingIndexBrush}">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="150" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Margin="5" TextWrapping="NoWrap" 
                       Style="{StaticResource BasicTextStyle}" 
                       VerticalAlignment="Center"
                       Text="{Binding Item.DateString}" />

            <TextBlock Grid.Column="1" Margin="5" TextWrapping="NoWrap" 
                       Style="{StaticResource BasicTextStyle}" 
                       VerticalAlignment="Center"
                       Text="{Binding Item.Description}" />

            <TextBlock Grid.Column="2" Margin="5" TextWrapping="NoWrap"
                       Style="{StaticResource BasicTextStyle}" 
                       VerticalAlignment="Center"
                       Text="{Binding Item.AmountStringCurrencyFormat}"
                       HorizontalAlignment="Right"/>

        </Grid>
    </DataTemplate>

<Style x:Key="SimpleListViewItemStyle" TargetType="ListViewItem">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="TabNavigation" Value="Local"/>
    <Setter Property="IsHoldingEnabled" Value="False"/>
    <Setter Property="IsDoubleTapEnabled" Value="False"/>
    <Setter Property="IsRightTapEnabled" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}" 
                            Margin="{TemplateBinding Margin}">

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <PointerDownThemeAnimation TargetName="Container"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Grid x:Name="Container">
                        <ContentPresenter x:Name="contentPresenter" 
                                              ContentTemplate="{TemplateBinding ContentTemplate}" 
                                              ContentTransitions="{TemplateBinding ContentTransitions}" 
                                              Content="{TemplateBinding Content}" 
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              Margin="{TemplateBinding Padding}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>