尝试通过 AutoCompleteBox 使用经典的 Wpf 结构(不使用 ViewModel)
因为我是新手WPF
,但还没有获得MVVM
..的知识
我的代码现在:
附言
对于此示例代码的用法,源文件应以制表符分隔,每一行如下:
a line No'(仅供参考),制表符分隔,AutoComplete的值。
- 每一行
- 有价值
- 由
- 制表符 = "\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="Images\IE_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="Images\Folder_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
初始可见性 = 隐藏。
然后,如果用户选择 item-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] }
);
}
}
}
}
问题是当我测试自动完成时,我使用过
ObservableCollection<URLsSrcClss> AutComplObsrvblCllctn = new ObservableCollection<URLsSrcClss>
{
new URLsSrcClss {url = "say yhoo..."},
new URLsSrcClss {url = "...google..."},
};
它确实有效
尽管更改要从大型数据源生成的代码是我猜是什么破坏了功能。至少我现在是这么想的。
任何想法我的代码有什么问题?