【问题标题】:Custom ListView Row template in WPFWPF 中的自定义 ListView 行模板
【发布时间】:2018-07-23 10:31:06
【问题描述】:

我是 WPF 应用程序开发的新手,没有 RelativeLayout(我可以在其中安排相对于另一个视图的视图......就像在 Android 中一样)。 我的列表视图行模板看起来像这样。

谁能帮我用代码来设计这样的 XAML 模板

编辑:这是我迄今为止尝试过的代码(仅适用于第一行,即名称、时间和日期)...它似乎不起作用。

 <ListView x:Name="listView" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="752" Margin="20,282,0,0" VerticalAlignment="Top" Width="486" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5*"/>
                        <ColumnDefinition Width="5*"/>
                        <!--<ColumnDefinition Width="120"/>-->
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}" FontWeight="Bold" />
                    <TextBlock Grid.Row="0" Grid.Column="1" FlowDirection="RightToLeft" Text="{Binding Time}" FontWeight="Bold" />
                    <!--<TextBlock Grid.Column="1" Background="Gainsboro" FlowDirection="LeftToRight" Text="{Binding Date}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />-->
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

时间和日期并排显示。我认为使用流向应该会有所帮助。

【问题讨论】:

  • 如果你能提供一些你迄今为止尝试过的代码,我们会更容易提供帮助。
  • 对不起...现在我已经添加了我正在处理的代码 sn-p。请检查
  • 您可能想要更详细地了解您需要帮助的部分。对于基本布局,我建议您查看 Grid 旁边的现有面板,尤其是 StackPanels 和 DockPanels 以及 Margin、Padding、Horizo​​ntalAlignment 和 VerticalAlignment 属性。

标签: c# wpf windows visual-studio xaml


【解决方案1】:

试试这个:

<DataTemplate>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}" FontWeight="Bold" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Time}" FontWeight="Bold" HorizontalAlignment="Right"/>

        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Header}" FontWeight="Bold" />
        <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Details}" FontWeight="Bold" />

    </Grid>
</DataTemplate>

注意:避免一些语法错误,因为我是在记事本中开发的。如您的问题中的图像所示,它没有任何滚动条。有问题可以评论。您可以根据需要应用文本格式和边距。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多