在带有 DataView 的 WPF DataGrid 中,我们希望将 ComboBox 显示为标签,直到单击为止

发布于 2024-11-28 09:16:30 字数 1454 浏览 0 评论 0原文

因此,我们有一个 WPF DataGrid,其中一些存在外部关系的单元格中包含一些组合框,但这意味着我们一次加载数百个组合框,这需要很长时间。我们想要做的是加载一个标签,直到单击它,此时加载一个 ComboBox,我们可以使用 TextBox 轻松地做到这一点。

问题是,我们的组合框工作并允许用户通过从多个显示值(例如{Car、Dog、Cat})中进行选择来更改列中的外键值。但在用户单击标签之前,标签会显示外键值本身(例如 {1, 2, 3})。

关于我们可以做什么的任何想法?任何帮助非常感谢!

<DataGridTemplateColumn Header="Column Name" SortMemberPath="Column Name"> 
    <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate>
            <Label Content="{Binding Path=DataViewBehindColumnName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
           <ComboBox 
            DataContext="{DynamicResource ResourceKey=ViewModel}" 
            ItemsSource="{Binding Path=ReferenceTableName, 
                         Converter={StaticResource dataViewToListConverter}}" 
            DisplayMemberPath="ReferenceTableDisplayNamesColumn" 
            SelectedValuePath="ReferenceTablePrimaryKeyColumn" 
            SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, 
                           Path=Item[DataViewBehindColumnName]}"
            />
        </DataTemplate> 
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

亲切的问候, 河豚

So we have a WPF DataGrid with some ComboBoxes in some of the cells where there is a foreign relation, but this means we have hundreds of ComboBoxes loaded at a time which takes too long. What we would like to do is load a label until it is clicked on at which point a ComboBox is loaded, we can easily do this with a TextBox.

The problem is, our comboboxes work and allow the user to change the foreign key value in the column by selecting from a number of display values (e.g. {Car, Dog, Cat}). But before the user clicks on the label, the label displays the foreign key value itself (e.g. {1, 2, 3}).

Any ideas on what we could do? Any help most appreciated!

<DataGridTemplateColumn Header="Column Name" SortMemberPath="Column Name"> 
    <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate>
            <Label Content="{Binding Path=DataViewBehindColumnName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
           <ComboBox 
            DataContext="{DynamicResource ResourceKey=ViewModel}" 
            ItemsSource="{Binding Path=ReferenceTableName, 
                         Converter={StaticResource dataViewToListConverter}}" 
            DisplayMemberPath="ReferenceTableDisplayNamesColumn" 
            SelectedValuePath="ReferenceTablePrimaryKeyColumn" 
            SelectedValue="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, 
                           Path=Item[DataViewBehindColumnName]}"
            />
        </DataTemplate> 
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Kind regards,
Fugu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送你一个梦 2024-12-05 09:16:30

每行创建一个 ViewModel,并提供一个表示解析的字符串值的属性和另一个表示键的属性。将标签 Content-property 绑定到 string-property,将组合框 SelectedValue 绑定到 key-property。

不是 MVVM 语言:创建一个行项目包装器,其中包含数据网格所需的所有属性,包括包含有问题的属性的已解析字符串值的属性。

Create a ViewModel per line and there in provide a Property that represents the resolved string value and another property that represents the key. Bind the labels Content-property to the string-property and the combobox SelectedValue to the key-property.

Not MVVM speaking: Create a line item-wrapper that holds all properties that are necessary for the datagrid including one that holds the resolved string-value of your problematic property.

零度° 2024-12-05 09:16:30

当我读到你的文章时,我想到了两件事:

1)DataGrid 虚拟化。这可以为您节省一些负担。

2a)使用布尔属性“ShowData”为您的数据对象构建代理。默认情况下此值为 false,单击控件时设置为 true。您可以使用 commandBinding 来做到这一点。如果您不使用 ORM,则可以扩展 DataSet SourceCode 并添加此功能。不过,这最终可能会成为一项繁重的工作。

2b)组合框有一个可编辑模板和一个不可编辑模板(您可以在 Blend 中轻松找到它们)。将不可编辑模板的内容绑定以显示静态字符串和可编辑字符串到关系。仅当您尝试编辑所选项目时,才会显示您的关系。同样,如果您使用像 NHibernate 这样的 ORM,您可以从 LazyLoading 功能中受益。

There's two things coming to my mind when I read your post

1) DataGrid Virtualization. This could save you some load.

2a)Build Proxies for your DataObjects with a bool property "ShowData". This is false by default and set to true when the control is clicked. You could do that with a commandBinding. If you're not using ORM, you can extend your DataSet SourceCode and add this functionality. This could end up being a sh*tload of work, though.

2b)ComboBoxes have an editable Template and a Non-Editable Template(You can find them easily in Blend).Bind the non-Editable Template's content to display a static String and the editable one to the relation. This will show your relation only when you try to edit the selectedItem. Again, if you use ORM like NHibernate, you can profit from LazyLoading features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文