【问题标题】:How can i find my WPF Autocompletebox in my DataGrid?如何在我的 DataGrid 中找到我的 WPF 自动完成框?
【发布时间】:2021-10-04 23:17:16
【问题描述】:

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

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

这个盒子正在工作。我可以在我的代码中找到“ACB2”框:

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

        }

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

<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>

Binding Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 运行良好。但我再也找不到我的代码中的 x:Name="ACB2" 了。谁能帮我这个?我尝试了许多不同的 AutoCompleteFunctions (Nuget),但我总是遇到建议绑定问题。也许有人对此有一些经验,可以帮助我吗?那将是非常非常好的。亲切的问候

【问题讨论】:

    标签: c# wpf autocomplete datagrid textbox


    【解决方案1】:

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

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

    XAML:

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

    在我的 Datagrid => Commandselection 是我编写列表的类,而 AutoCompleteBoxItems 是列表的名称

    <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"
                };
            }
            
        }
    

    【讨论】:

      【解决方案2】:

      有很多方法可以做到这一点,但我会告诉你最简单的。将 x:Name 放入您的 DataGrid 并获取它的第一个孩子,这样它将成为您的文本框。如果你有更难实现的东西,那么看看 MVVM 和 DataBinding...它的信息获取途径很长,但在使用 WPF 时了解它是正确的。

          public List<string> AutoCompleteBoxItems { get; set; } = new List<string>();
          
          public Databasewindow()                                                  
          {
              InitializeComponent();
          
              AutoCompleteBoxItems.Add("Test1");
              AutoCompleteBoxItems.Add("Test2");
          }
      

      XAML

              <controls:AutoCompleteBox Text="{Binding Command, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
          ItemsSource="{Binding AutoCompleteBoxItems}"
      FilterMode="Contains"></controls:AutoCompleteBox>
      

      您也可以使用此method 来查找您的 UIElement,但在 DatGrid.Loaded 事件中执行此操作。您始终可以扩展该方法并找到所有相同的 UIElement,但这又不是您应该这样做的方式。使用 DataBinding insted。强烈推荐。

      【讨论】:

      • 您好 Melkor V,感谢您的回复。你能给我一个例子吗?亲切的问候
      • 对于这种方法,您需要 DataGrid 中的 ItemsSource 属性。解决问题的另一种方法是为您的 AutoCompleteBox 绑定 ItemsSource,例如: 其中 CollectionOfItems 是一个集合,其中 { "TEST1", "TEST2 TEST2", "TEST3 TEST3" } 在您的 DataBaseWindow 中作为 get/set 的公共属性
      • 哇,非常感谢!明天我会测试它,当我再次回到工作岗位时。你拯救了我的一天。当我在编程中遇到问题时,我无法停止思考xD
      • @MarioAllw 不客气。请记住将答案标记为已接受 x)
      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2010-10-31
      • 1970-01-01
      • 2010-10-15
      • 2015-03-13
      • 1970-01-01
      相关资源
      最近更新 更多