Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-GridView.GridViewColumn每行两行?_C#_Wpf_Gridview_Listview_Mvvm - Fatal编程技术网

C# WPF-GridView.GridViewColumn每行两行?

C# WPF-GridView.GridViewColumn每行两行?,c#,wpf,gridview,listview,mvvm,C#,Wpf,Gridview,Listview,Mvvm,您好,是否可以有一个ListView->ListView.View->GridView->GridView列,每行有“两行”。 如。 第1列|第2列 第1行废话|数据废话 第2排等|更多 我尝试使用单元格模板失败,但手动调整其包含列的大小时,模板中的项不会调整大小 代码: 有什么想法吗 编辑:这是在使用MVVM,因此在一个有界的ViewModel后面没有代码请容忍我,因为我不能100%确定这是您想要的。但是,假设您希望每一行下面都有更多的文本(a la the),我想我可以提供帮助 您需

您好,是否可以有一个ListView->ListView.View->GridView->GridView列,每行有“两行”。
如。
第1列|第2列
第1行废话|数据
废话
第2排等|更多

我尝试使用单元格模板失败,但手动调整其包含列的大小时,模板中的项不会调整大小

代码:


有什么想法吗


编辑:这是在使用MVVM,因此在一个有界的ViewModel后面没有代码

请容忍我,因为我不能100%确定这是您想要的。但是,假设您希望每一行下面都有更多的文本(a la the),我想我可以提供帮助

您需要覆盖
ListViewItem
的默认样式,并更改
ControlTemplate
以在
GridViewRowPresenter
下添加
TextBlock
。完成后,它将看起来像这样:

<Style TargetType="ListViewItem">
   <Setter Property="SnapsToDevicePixels" Value="true"/>
   <Setter Property="OverridesDefaultStyle" Value="true"/>
   <Setter Property="Template">
   <Setter.Value>
      <ControlTemplate TargetType="ListBoxItem">
         <Border Name="Border"
                 Padding="2"
                 SnapsToDevicePixels="true"
                 Background="Transparent">
            <StackPanel>
               <GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}"  
                                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
               <TextBlock Text="This is where your text goes!"/>
            </StackPanel>
         </Border>

         <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="true">
               <Setter TargetName="Border"
                       Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
               <Setter Property="Foreground" 
                       Value="{StaticResource DisabledForegroundBrush}"/>
            </Trigger>
         </ControlTemplate.Triggers>
         </ControlTemplate>
   </Setter.Value>
   </Setter>
</Style>


您可能需要绑定
文本块的
宽度
,以便将其包装在
列表视图中
,但这应该可以让您开始了。

好的,我再试一次。您是否尝试过使用
TextBlock
并将
TextWrapping
设置为
Wrap
CellTemplate

例如:

<ListView x:Name="MyListView">
   <ListView.View>
      <GridView>
         <GridViewColumn Header="Hello"
                         Width="50">
            <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce non nibh quis odio aliquet venenatis."
                             TextWrapping="Wrap"/>
               </DataTemplate>
            </GridViewColumn.CellTemplate>
         </GridViewColumn>
         <GridViewColumn Header="World"/>
      </GridView>
   </ListView.View>

   <ListViewItem/>
   <ListViewItem/>
   <ListViewItem/>
   <ListViewItem/>
</ListView>

首先,CellTemplate是在单元格中拥有自定义内容的方法。例如,您可以在CellTemplate中有一个垂直的StackPanel

接下来,要在更改列宽时自动调整单元格内容的大小,需要在ListViewItem上指定HorizontalContentAlignment=“Stretch”:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

我就是这么做的


我觉得这是一个简单的解决方案,可以解决很多人遇到的一个问题,即每个记录有2行,但仍然能够使用gridview控件附带的分页和排序等功能。我会给你的代码,并试图解释它尽我所能


消息

诀窍在于自定义TemplateField ItemTemplate

首先要知道的是,当您创建TemplateField时,html源代码中呈现的是一个新的打开和关闭TD标记。创建最后一列后,它将使用结束标记关闭该行。 因此,创建templatefield open和close以及open和close ItemTemplate标记。在这里面,您要做的第一件事是关闭opentd标记“”,然后关闭opentr标记“”。然后创建一个新行和一个新列标记。现在,如果某个描述或注释列需要宽一点。使用columnspan=10或需要的列数。因为Templatefield将关闭TD标记,所以最后一个TD标记不应该有结束标记,最后一行不应该有结束TR标记,因为这将在结束标记所在的位置关闭


我希望这有助于人们理解,并且足够清晰。可以使用此方法创建每个记录所需的任意行。如果你对html和表格很在行的话,也可以做一些其他的格式化工作。关键是要记住,当你开始时,你正在一个打开的td和tr标记中格式化,最后它会在一个关闭的td和tr标记中写入,所以让你的最后一列和最后一行保持打开状态。

我不认为这是我要找的ascalonx。它似乎只是在每一行中添加一个静态行。我希望将长内容推到第二行。只在一列之内。哦…对不起,我误解了。我必须考虑一下,看看我是否能想出一个解决办法。这是一个老问题,但这正是我一直在寻找的。不知道简单地问一个新问题的礼节,所以这个答案是分开的——有点代表囤积的味道。嘿,decasteljau,你的答案和ascalonx的答案相结合就是解决办法。干杯。嘿,阿斯卡隆你的答案和迪卡斯泰勒约的答案相结合就是解决办法。干杯
<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>
            <GridView ColumnHeaderToolTip="Addendum Master" >
                    <GridViewColumn Width="500">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>                                    
                                <StackPanel Orientation="Vertical" Margin="0">
                                    <Label Content="{Binding Path=Line1}" Padding="0" Margin="0" Style="{StaticResource GridLabelStyle}"></Label>
                                    <Label Content="{Binding Path=Line2}" Padding="0" Margin="0" Style="{StaticResource GridLabelStyle}"></Label>
                                    <Label Content="{Binding Path=Line3}" Padding="0" Margin="0" Style="{StaticResource GridLabelStyle}"></Label>
                                    <Label Content="{Binding Path=Line4}" Padding="0" Margin="0" Style="{StaticResource GridLabelStyle}"></Label>
                                </StackPanel>                                                                        
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
    <Columns>                            
        <asp:BoundField DataField="CPTCode" HeaderText="CPTCode" ItemStyle-CssClass="center" SortExpression="CPTCode" >
            <ItemStyle CssClass="center" />
        </asp:BoundField>

            <asp:BoundField DataField="InsPlanCode" HeaderText="InsPlanCode" ItemStyle-CssClass="center" SortExpression="InsPlanCode" >
            <ItemStyle CssClass="center" />
        </asp:BoundField>

            <asp:BoundField DataField="StartDate" HeaderText="Start Date" ItemStyle-CssClass="center" SortExpression="StartDate" >
            <ItemStyle CssClass="center" />
        </asp:BoundField>

            <asp:BoundField DataField="EndDate" HeaderText="End Date" ItemStyle-CssClass="center" SortExpression="EndDate" >
            <ItemStyle CssClass="center" />
        </asp:BoundField>

        <asp:BoundField DataField="UserName" HeaderText="UserName" ItemStyle-CssClass="center" >
            <ItemStyle CssClass="center" />
        </asp:BoundField>   

        <asp:TemplateField HeaderText=" " ItemStyle-CssClass="center" HeaderStyle-BackColor="AliceBlue" ItemStyle-BackColor="AliceBlue">                                
            <ItemTemplate>        

                </td>
                </tr>
                <tr>
                    <td align="center" nowrap>&nbsp;<b>Message</b>&nbsp;</td>
                    <td colspan="10"><TABLE cellpadding=5><tr><td><%# Eval("carveOutMessage")%></td></tr></TABLE>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>