3

亲爱的用户:我有一个数据网格视图,我在一个带有 c 锐利的 Windows 窗体中使用,这个数据网格视图有如下列:

  • 姓名
  • 价格
  • 细节

我有可以为每个单元格设置值的按钮,一行只能包含一个名称和价格,但有几个详细信息行,为了达到目的,我用 environment.newline 分隔每个条目,以防条目用于详细信息。

在此处输入图像描述

好的,所以我希望你明白这一点,现在真正有趣的部分是我希望用户能够单击并选择该项目的 datagriview 行内的这些子行之一。此数据网格也未绑定到任何表。这可以做到吗?谢谢大家先进

4

2 回答 2

2

发布此答案是因为 OP 要求它。

这就是您在 WPF 中执行此操作的方式:

<Window x:Class="MiscSamples.ListBoxInCell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListBoxInCell" Height="300" Width="300">
    <DockPanel>
        <Button Content="Show Selected Detail" DockPanel.Dock="Bottom"
                Click="ShowDetail"/>

        <ListView ItemsSource="{Binding Items}"
              SelectedItem="{Binding SelectedItem}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Producto" DisplayMemberBinding="{Binding Product}"/>
                    <GridViewColumn Header="Detalle">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ListBox ItemsSource="{Binding Details}"
                                     SelectedItem="{Binding SelectedDetail}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Window>

代码背后:

public partial class ListBoxInCell : Window
{
    public ViewModel ViewModel { get; set; }

    public ListBoxInCell()
    {
        InitializeComponent();

        DataContext = ViewModel = new ViewModel();
    }

    private void ShowDetail(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(ViewModel.SelectedItem.SelectedDetail);
    }
}

视图模型:

public class ViewModel
{
    public List<Data> Items { get; set; }

    public Data SelectedItem { get; set; }

    public ViewModel()
    {
        //Sample Data
        Items = Enumerable.Range(0, 100).Select(x => new Data
            {
                Product = "Product" + x.ToString(),
                Details = Enumerable.Range(0, 3)
                                    .Select(d => "Detail" + x.ToString() + "-" + d.ToString())
                                    .ToList()
            }).ToList();
        SelectedItem = Items.First();
    }
}

数据项:

public class Data
{
    public string Product { get; set; }

    public List<string> Details { get; set; } 

    public string SelectedDetail { get; set; }
}

结果:

在此处输入图像描述

  • MVVM,这意味着数据与 UI 分离,UI 与数据分离。
  • 没有“所有者抽奖”,没有“P/Invoke”(不管那是什么意思),也没有可怕的程序黑客。只有漂亮的 XAML 和 DataBinding。
  • 每行内的每个选择ListBox都是单独的,您可能希望通过null简单的 LINQ 查询和迭代将所有其他项目设置为仅保留 1 个选定项目。
  • 单击按钮可查看 ListView 中当前选定行的当前选定详细信息。
  • WPF Rocks,将我的代码复制并粘贴到 a 中File -> New Project -> WPF Application,然后自己查看结果。
  • 忘记winforms。没用的。它不支持任何级别的自定义,并且需要对所有内容进行大量可怕的黑客攻击。它不支持(真正的)DataBinding,并迫使您采用无聊的程序方法。
于 2013-09-23T03:58:00.593 回答
0

DataGridView don't supports sub-rows in a row. So, my advice is that you should use two DataGridViews:

  • One contains list of Names and Prices.
  • Another one display list of Detail lines that belong to current selected row in the first DataGridView.
于 2013-09-20T22:28:11.240 回答