如何在我的 DataGrid 中找到我的 WPF 自动完成框? [英] How can i find my WPF Autocompletebox in my DataGrid?

查看:25
本文介绍了如何在我的 DataGrid 中找到我的 WPF 自动完成框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,几天来我一直在尝试将 Autocompletebox 集成到我的项目中,但不幸的是没有成功.我有点绝望了.我至少为我的 Datagrid 之外的文本框找到了一个非常简单的解决方案(DotNetProjects.WpfToolkit.Input 6.0.90 https://www.nuget.org/packages/DotNetProjects.WpfToolkit.Input/) 但我不能把它放在我的 .我按照此视频中的说明进行操作:https://www.youtube.com/watch?v=SK8TXvcPWqI.我将在这里发布我的代码:

Hello I've been trying to integrate an Autocompletebox into my project for days, unfortunately without success. I'm getting a little desperate. I have at least found a very simple solution for a text box outside of my Datagrid (DotNetProjects.WpfToolkit.Input 6.0.90 https://www.nuget.org/packages/DotNetProjects.WpfToolkit.Input/) but I can't put it in my . I followed the instructions in this video: https://www.youtube.com/watch?v=SK8TXvcPWqI. I will post my Code here:

<Grid>
<controls:AutoCompleteBox x:Name="ACB2" Text="TT" ></controls:AutoCompleteBox> 
</Grid>

这个盒子正在工作.我能找到盒子ACB2"在我的代码中:

This Box is working. I Can find the Box "ACB2" in my Code:

public Databasewindow()                                                  
        {
            InitializeComponent();
            ACB2.FilterMode = AutoCompleteFilterMode.Contains;
            ACB2.ItemsSource = new string[] { "TEST1", "TEST2 TEST2", "TEST3 TEST3" };

        }

但是当我尝试填充一列并使用此框时,我失败了并且再也找不到我的文本框

But when i try to fill a Column and use this box i fail and cant find my Textbox anymore

<Grid>
<DataGrid x:Name="DataGrid1"
    <DataGridTemplateColumn  Width="200" Header=" Command ">
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <controls:AutoCompleteBox 
                                Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                x:Name="ACB2" 
                                ></controls:AutoCompleteBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
           </DataGrid>
         </Grid>

绑定文本={绑定命令,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}";工作良好.但我找不到 x:Name="ACB2"在我的代码中了.谁能帮我这个?我尝试了许多不同的 AutoCompleteFunctions (Nuget),但我总是遇到建议绑定的问题.也许有人对此有一些经验,可以帮助我吗?那会非常非常好.亲切的问候

The Binding Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" is working good. But i cant find the x:Name="ACB2" in my Code anymore. Can anyone help me with this? I tried many different AutoCompleteFunctions (Nuget) but i always had problems with the suggestions-Bindings. Maybe someone has some experience with this and can can help me out? That would be very, very nice. Kind regards

推荐答案

所以我找到了解决绑定问题的方法.感谢 Melkor V 的支持.这是我的代码:

So i found a solution for my Binding problem. Thank you Melkor V for your support. Here is my Code:

在我的 Xaml 中,我必须为我的绑定添加这些行:

In my Xaml i had to add these lines for my Binding:

XAML:

xmlns:controls='clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Input.Toolkit' xmlns:local="clr-namespace:Databases"

在我的数据网格中 =>Commandselection 是我编写列表的类,AutoCompleteBoxItems 是列表的名称

In my Datagrid => Commandselection is the Class where i wrote a List and AutoCompleteBoxItems is the Name of the List

<DataGrid 
   <DataGrid.DataContext>
         <local:Commandselection/>
   </DataGrid.DataContext>
</DataGrid>

<DataGridTemplateColumn  Width="200" Header=" Command ">
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <controls:AutoCompleteBox 
                                Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                ItemsSource="{Binding Path = DataContext.AutoCompleteBoxItems, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                FilterMode="Contains" 
                                ></controls:AutoCompleteBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

这是我班级的代码:

class Commandselection 
    {

        public static ObservableCollection<string> AutoCompleteBoxItems { get; set; }
        public Commandselection()
        {
            AutoCompleteBoxItems = new ObservableCollection<string>()
            {
                "Apple",
                "Banana",
                "Carrot",
                "Dog",
                "Elderberry",
                "Fruit",
                "Grapes",
                "Honey",
                "Iron"
            };
        }
        
    }

这篇关于如何在我的 DataGrid 中找到我的 WPF 自动完成框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆