Question

trying to use classic Wpf structure via AutoCompleteBox (not using ViewModel)

as i am new to WPF and yet to acquire knowledge of MVVM..

My Code For now :

P.S

for usage on this sample code, source file should be - tab separated,each line would be as follows:

a line No' (only for reference), tab separation , value for AutoComplete.

  1. every line
  2. has value
  3. separated by
  4. Tab = "\t"

XAML:

Main declaration - window

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

a Grid within a TabItem (i guess rest is irrelevant):

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

that's where i needed the autoCompletebox To be placed. ACB initial Visibility = hiden.
then, if user selects item-internet, this ACB Will turn To Visible.

(just the idea, not implemented here at this stage)

C# AutoComplete code.

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] }
                );
            }
        }
    }
}

the problem is that when i have tested autoComplete i have used

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

it did work

though changing code to be generated out of a large data source is i guess what's breaking functionality. that's at least what i think for now.

any ideas what is wrong in my code?

Was it helpful?

Solution

it is working now I just made a mistake not initializing it properly the code above will not work , I've decided to leave it as a question..with my answer as well.. for future comers sake .

so any fresh developers that has WPF AutoCompleteBox implemetation issues could have this nice solution to solve the problem.

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

having said that, your welcome to share your thoughts, ideas,critics or tips...

so if you have any, please do comment it here.

thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top