2

我的表代码

ItemS 控件工作完美但我只需要一个列表视图内的列表视图帮助我解决问题

c Get Set Class 我需要一个单一的列表

嗨,我需要在我的 Metro 应用程序中生成一个表格,但我得到了错误的输出,请帮助我分别使用列表绑定标题和内容

C# 代码

 foreach (var item in itemsreceiveds)
        {
            variable2.Add(new contentdata() { firstdata = item.getFirstData, seconddata = item.getSecondData, thirddata = item.getThirdData, headers = item.getFieldName });
        }
        var groupedPersons = variable2.Select((emp) => new { sectionName = emp.headers }).ToList().Distinct();
         foreach (var s in groupedPersons)
         {
             firstheader = s.sectionName;
             string[] sepword = firstheader.Split('#');
             firstheader = sepword[0].ToUpper();
             secondheader = sepword[1].ToUpper();
             thirdheader = sepword[2].ToUpper();  
             variableheader.Add(new contentdataheader(firstheader,secondheader,thirdheader));
             groupListView.ItemsSource = variableheader;
         }
         //SUBITEM
          foreach (var person in variable2)
         {
             subdata.Add(new subitems(person.firstdata, person.seconddata, person.thirddata));
         }
         itemListViewss.ItemsSource = subdata;

XAML

        <ListView Name="groupListView" CanDragItems="True" CanReorderItems="True" AllowDrop="True" IsSwipeEnabled="True" SelectionMode="None" Grid.ColumnSpan="3" Grid.Row="0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" >
                    <Border x:Name="bordermenu" BorderBrush="Black" BorderThickness="1" Grid.Column="1" Grid.Row="0" Width="150">
                        <TextBlock Foreground="Red" Text="{Binding Firstheader}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <Border x:Name="bordermenu2" BorderBrush="Black" BorderThickness="1" Grid.Column="2" Grid.Row="0" Width="150">
                        <TextBlock Foreground="Red" Text="{Binding Secondheader}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <Border x:Name="bordermenu3" BorderBrush="Black" BorderThickness="1" Grid.Column="3" Grid.Row="0" Width="150">
                        <TextBlock Foreground="Red" Text="{Binding Thirdheader}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <ListView Name="itemListViewss" CanDragItems="True" CanReorderItems="True" AllowDrop="True" IsSwipeEnabled="True" SelectionMode="None" Grid.ColumnSpan="3" Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" >
                    <Border x:Name="bordersubmenu" BorderBrush="Black" BorderThickness="1" Grid.Column="1" Grid.Row="1" Width="150">
                        <TextBlock Foreground="Blue" Text="{Binding dynamic}" x:Name="submenu" HorizontalAlignment="Center" VerticalAlignment="Center"   />
                    </Border>
                    <Border x:Name="bordersubmenu2" BorderBrush="Black" BorderThickness="1" Grid.Column="2" Grid.Row="1" Width="150">
                        <TextBlock Foreground="Blue" Text="{Binding p1}" x:Name="submenu2"  HorizontalAlignment="Center" VerticalAlignment="Center"  />
                    </Border>
                    <Border x:Name="bordersubmenu3" BorderBrush="Black" BorderThickness="1" Grid.Column="3" Grid.Row="1" Width="150">
                        <TextBlock Foreground="Blue" Text="{Binding p2}" x:Name="submenu3" HorizontalAlignment="Center" VerticalAlignment="Center"  />
                    </Border>                        
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我得到输出

我得到输出

但我需要这样的输出

但我需要这样的输出

4

3 回答 3

22

这是最简单的答案 - 一个工作演示。

我并没有为你做任何事,伙计,但我确实做了很多。您必须编写一些代码才能以您喜欢的方式获得它。如果您使用的是自定义控件套件,则大部分操作可能会为您完成。如果没有,这将使您走上正轨。远在路上。

把这段代码放在后面:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
}

public class Datum
{
    public DateTime Date { get; set; }
    public string Year { get { return Date.ToString("yyy"); } }
    public string Month { get { return Date.ToString("MMMM"); } }
    public string Day { get { return Date.ToString("dd"); } }
    public string Weekday { get { return Date.ToString("dddd"); } }
}

public class ViewModel
{
    public ViewModel()
    {
        // data
        var _Data = Enumerable.Range(1, 20)
            .Select(x => new Datum { Date = DateTime.Now.Add(TimeSpan.FromDays(x * 14)) });
        Data = new ObservableCollection<Datum>(_Data);
    }
    public ObservableCollection<Datum> Data { get; private set; }
}

然后试试这个 XAML:

<Page.DataContext>
    <local:ViewModel/>
</Page.DataContext>

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Grid Width="400">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="100" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.Resources>
                <Style TargetType="Border">
                    <Setter Property="BorderBrush" Value="Black" />
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Padding" Value="5" />
                </Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="Black" />
                </Style>
            </Grid.Resources>
            <Border Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="4">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
                    <TextBlock VerticalAlignment="Center" TextAlignment="Right">
                        <Run Text="Search" />
                        <LineBreak />
                        <Run Text="Data" />
                    </TextBlock>
                    <TextBox Width="100" />
                </StackPanel>
            </Border>
            <Border Grid.Column="0" Grid.Row="1"><TextBlock Text="Year" /></Border>
            <Border Grid.Column="1" Grid.Row="1"><TextBlock Text="Month" /></Border>
            <Border Grid.Column="2" Grid.Row="1"><TextBlock Text="Day" /></Border>
            <Border Grid.Column="3" Grid.Row="1"><TextBlock Text="Weekday" /></Border>
        </Grid>
        <ItemsControl ItemsSource="{Binding Data}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Width="400">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="Margin" Value="5,0" />
                            </Style>
                        </Grid.Resources>
                        <TextBlock Grid.Column="0" Text="{Binding Year}" />
                        <TextBlock Grid.Column="1" Text="{Binding Month}" />
                        <TextBlock Grid.Column="2" Text="{Binding Day}" />
                        <TextBlock Grid.Column="3" Text="{Binding Weekday}" />
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Grid Width="400">
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.Resources>
                <Style TargetType="Border">
                    <Setter Property="BorderBrush" Value="Black" />
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Padding" Value="5" />
                </Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="Black" />
                </Style>
            </Grid.Resources>
            <Border Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="4">
                <TextBlock Text="Showing 1 to 10 of 371" HorizontalAlignment="Right" />
            </Border>
        </Grid>
    </StackPanel>
</Grid>

你应该得到这样的东西:

在此处输入图像描述

于 2013-07-29T21:03:42.523 回答
2

在他的回答中添加到 Jerry 的 XAML 中。您可以将 ItemsControl 替换为 ListBox(相同的模板/itemTemplate 修改)以获得相同的视图,还可以使用 SelectedIndex,但我不知道这是否适用于这个问题。

注意:我确实发现您需要向 ListBox 添加一个宽度。您不能只让 ItemTemplate 中的 Grid 来做。

于 2014-07-28T19:59:06.090 回答
0

也许你正在寻找这样的东西。

XAML

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Rectangle Fill="Black" Grid.ColumnSpan="2" Grid.Row="0" />
    <TextBlock Text="Integer" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="0" Grid.Column="0" FontSize="14" />
    <TextBlock Text="String" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="0" Grid.Column="1" FontSize="14" />
    <ListView x:Name="ListViewDisplay" ItemsSource="{Binding}" Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="Black" Background="White" RequestedTheme="Light">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="150" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Integer}" Margin="5" Grid.Column="0" FontSize="14" />
                    <TextBlock Text="{Binding String}" Margin="5" Grid.Column="1" FontSize="14" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

后面的代码

public sealed partial class SamplePage : Page
{
    ObservableCollection<ROWS> r = new ObservableCollection<ROWS>();
    public SamplePage()
    {
        this.InitializeComponent();
        r.Add(new ROWS { Integer = 1, String = "ONE" });
        r.Add(new ROWS { Integer = 2, String = "TWO" });
        r.Add(new ROWS { Integer = 3, String = "THREE" });
        r.Add(new ROWS { Integer = 4, String = "FOUR" });
        r.Add(new ROWS { Integer = 5, String = "FIVE" });
        ListViewDisplay.ItemsSource = r;
    }
}

public class ROWS {
    public int Integer { get; set; }
    public string String { get; set; }
}

结果

结果

于 2016-02-12T09:28:15.923 回答