如何将自动完成框添加到数据网格?

发布于 2024-11-02 16:03:22 字数 3352 浏览 0 评论 0原文

我的应用程序中有一个数据网格,包含两列和 4 行。每个单元格都需要是 WPF Toolkit 自动完成框控件。我想使用 MVVM 模式来实现整个事情。使用文本框填充单元格很容易:

<DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>                    
                <DataGridTextColumn Header="Predicate" Binding="{Binding Key}"/>                
                <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
            </DataGrid.Columns>
</DataGrid>

但尝试对自动完成框执行相同操作由于某种原因不起作用。它可以编译并运行,但键入时不会打开自动完成框。我使用:

<DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Pre">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <wpfToolkit:AutoCompleteBox ItemsSource="{Binding viewModel, Path=AvailableFields}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
</DataGrid>

绑定属性的定义是:

public IEnumerable<KeyValuePair<string, string>> Fields
public IEnumerable<string> AvailableFields

有什么想法吗?

更新:以下是基于@Damascus回复的完整XAML。由于某种原因也不起作用。

<UserControl x:Class="IKB.Views.IKBInputView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:IKB.ViewModels"
             xmlns:wpfToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <vm:IKBInputVM x:Key="viewModel" />
    </UserControl.Resources>
    <StackPanel Orientation="Vertical">        
        <DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Pre">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <wpfToolkit:AutoCompleteBox ItemsSource="{Binding DataContext.viewModel.AvailableFields, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</UserControl>

I have a datagrid in my application with two columns and 4 rows. Each of the cells needs to be a WPF Toolkit autocompletebox control. I'd like to implement the whole thing using the MVVM pattern. It's easy to populate the cells with text boxes with:

<DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>                    
                <DataGridTextColumn Header="Predicate" Binding="{Binding Key}"/>                
                <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
            </DataGrid.Columns>
</DataGrid>

But trying to do the same with autocomplete boxes doesn't work for some reason. It compiles and runs but the autocomplete box doesn't open when typing. I used:

<DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Pre">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <wpfToolkit:AutoCompleteBox ItemsSource="{Binding viewModel, Path=AvailableFields}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
</DataGrid>

The definitions for the bound properties are:

public IEnumerable<KeyValuePair<string, string>> Fields
public IEnumerable<string> AvailableFields

Any ideas?

Update: The following is the entire XAML based on @Damascus reply. Doesn't work either for some reason.

<UserControl x:Class="IKB.Views.IKBInputView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:IKB.ViewModels"
             xmlns:wpfToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <vm:IKBInputVM x:Key="viewModel" />
    </UserControl.Resources>
    <StackPanel Orientation="Vertical">        
        <DataGrid ItemsSource="{Binding viewModel, Path=Fields}"  AutoGenerateColumns="False" CanUserResizeColumns="True" CanUserSortColumns="True" AlternatingRowBackground="Gainsboro">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Pre">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <wpfToolkit:AutoCompleteBox ItemsSource="{Binding DataContext.viewModel.AvailableFields, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</UserControl>

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

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

发布评论

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

评论(1

顾冷 2024-11-09 16:03:22

这是因为您不在此处的同一个 DataContext 中。

每个AutoCompleteBox都会有自己的DataContext(不确定它的确切位置,但它与当前单元格本身有关),因此它会以错误的方式查找ItemsSource地方。

您的 Binding 必须引用原始 DataContext。
假设此 DataGrid 位于 UserControl 中,请尝试:

<wpfToolkit:AutoCompleteBox ItemsSource="{Binding DataContext.viewModel.AvailableFields,
           RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}} />

这应该可以解决问题(它现在将查看 UserControl 的 DataContext,这是您正在使用的 DataContext)

It is because you are not in the same DataContext here.

Each AutoCompleteBox will have its own DataContext (not sure about its exact position, but it is related to the current cell itself) , so it will look for the ItemsSource in a wrong place.

Your Binding has to refer to the original DataContext.
Assuming that this DataGrid is in a UserControl, try:

<wpfToolkit:AutoCompleteBox ItemsSource="{Binding DataContext.viewModel.AvailableFields,
           RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}} />

This should do the trick (it will now look into the UserControl's DataContext, which is the one you are working with)

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