AutoCompleteBox的DataSource大小超过10k条记录



尝试通过AutoCompleteBox(不使用ViewModel)使用经典Wpf结构

因为我是WPF的新手,还没有获得MVVM的知识。。

我的代码目前:

第页。S

为了在该示例代码上使用,源文件应该以制表符分隔,每行如下:

a行"否"(仅供参考),制表符分隔,自动完成值。

  1. 每行
  2. 具有值
  3. 由分隔
  4. 制表符="\t">

XAML:

主申报-窗口

// this happens to be a borderless - window with custom buttons
<Window x:Class="AllDayWpf.MainWindow" Icon="/AllDayWpf;component/bin/Debug/ScheduledTICON.png"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:acb="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns:System="clr-namespace:System;assembly=mscorlib" 
Title="Daily Tasks Srv-2" Height="555" Width="731" 
ResizeMode="NoResize" 
WindowStyle="None"
AllowsTransparency="True" Background="{x:Null}"> 
<Window.Effect>
<DropShadowEffect Opacity="0.5" BlurRadius="5" ShadowDepth="5" />
</Window.Effect>

TabItem中的网格(我想其余部分无关紧要):

<Grid>
<ListBox Name="LBX_AddTaskOptions" SelectionChanged="LBX_AddTaskOptions_SelectionChanged"  HorizontalAlignment="Left" Margin="19,29,0,0"  VerticalAlignment="Top" Width="125" FontWeight="Bold" Background="Beige">
<ListBoxItem Background="#9B6ADBCD" FontWeight="Bold" BorderBrush="#FF27AA27">
<StackPanel Orientation="Horizontal">
<TextBlock Text="internet" Width="74"></TextBlock>
<Image Source="ImagesIE_BlackRed.png" Height="30"></Image>
</StackPanel>
</ListBoxItem>
<ListBoxItem Background="#9B6ADBCD" FontWeight="Bold" BorderBrush="#FF27AA27">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Local Folder" Width="74"></TextBlock>
<Image Source="ImagesFolder_Black.png" Height="30" Width="32"></Image>
</StackPanel>
</ListBoxItem>
</ListBox>
<acb:AutoCompleteBox Name="UrlACBXml" ValueMemberPath="url" HorizontalAlignment="Left" Grid.Column="1" Width="296" Margin="150,23,0,156" Background="#FFEDF4AB">
<acb:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding url}" FontWeight="Bold" Foreground="Black" Width="30"/>
</StackPanel>
</DataTemplate>
</acb:AutoCompleteBox.ItemTemplate>    
</acb:AutoCompleteBox>
</Grid>

那就是我需要放置autoCompletebox的地方。ACB初始可见性=隐藏
然后,如果用户选择项目internet,则此ACB将变为可见。

(只是这个想法,现阶段没有在这里实现)

C#自动完成代码。

public MainWindow()
{
InitializeComponent();
MyUrlObsrvblCollcntFactory UrlObsrCollcntFctry= new MyUrlObsrvblCollcntFactory();
UrlACBXml.ItemsSource = UrlObsrCollcntFctry.AutComplObsrvblCllctn;
}
public class MyUrlObsrvblCollcntFactory
{
public class URLsSrcClss
{
public string url { get; set; }
}
String path = System.IO.Path.Combine(Environment.CurrentDirectory,"tst.txt");
string[] testit;
public ObservableCollection<URLsSrcClss> AutComplObsrvblCllctn { get; set; }
public MyUrlObsrvblCollcntFactory()
{
if (File.Exists(path))
{
testit = File.ReadLines(path).ToArray();
foreach (var item in testit)
{
var TabSeparatedItemArr = item.Split('t');
this.AutComplObsrvblCllctn.Add( new URLsSrcClss { url = TabSeparatedItemArr[1] }
);
}
}
}
}

问题是,当我测试autoComplete时我用过

ObservableCollection<URLsSrcClss> AutComplObsrvblCllctn = new ObservableCollection<URLsSrcClss> 
{
new URLsSrcClss {url = "say yhoo..."},
new URLsSrcClss {url = "...google..."},
};

它确实起到了的作用

尽管正在更改要从大型数据源生成的代码我想是什么破坏了功能。这至少是我目前的想法。

你知道我的代码出了什么问题吗?

它现在正在工作,我只是犯了一个错误,没有正确初始化它上面的代码不起作用,我决定把它作为一个问题。。还有我的答案。。为了将来的人。

因此,任何有WPF AutoCompleteBox实现问题的新开发人员可以有这个很好的解决方案来解决这个问题。

public ObservableCollection<URLsSrcClss> AutComplObsrvblCllctn = new ObservableCollection<URLsSrcClss>();

话虽如此,欢迎您分享您的想法、想法、评论家或建议。。。

所以,如果你有任何,请在这里评论。

谢谢。

最新更新