尝试通过AutoCompleteBox使用经典的Wpf结构(不使用ViewModel)
因为我是WPF的新手,还没有学到MVVM的知识。
现在我的代码:
P.S
为了在这个示例代码中使用,源文件应该是- tab分隔的,每一行如下所示:
一行No‘(仅供参考),制表符分隔,AutoComplete值。
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中的网格(我猜rest与此无关):
<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初始能见度=隐藏。
然后,如果用户选择项目-互联网,这个ACB将变成可见的.
(只是这个想法,目前还没有在这里实现)
C# AutoComplete代码。
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..."},
};确实起作用了
虽然修改要从大型数据源生成的代码是我想要破坏的功能。至少我现在是这么想的。
我的代码有什么问题吗?
发布于 2012-12-01 04:06:12
它正在工作--我只是犯了一个错误--没有正确地初始化它--上面的代码将不能工作,我决定把它作为一个question..with --我的答案也是。为了未来的到来。
因此,任何有WPF AutoCompleteBox实现问题的新开发人员都可以有这个很好的解决方案来解决这个问题。
public ObservableCollection<URLsSrcClss> AutComplObsrvblCllctn = new ObservableCollection<URLsSrcClss>();话虽如此,欢迎你分享你的想法、想法、批评或建议.
因此,如果你有,请在这里评论它。
谢谢。
https://stackoverflow.com/questions/13655956
复制相似问题