Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoCompleteTextBlock #1

Open
rafaqat-ali-confiz opened this issue Nov 12, 2013 · 4 comments
Open

AutoCompleteTextBlock #1

rafaqat-ali-confiz opened this issue Nov 12, 2013 · 4 comments

Comments

@rafaqat-ali-confiz
Copy link

Hi hermitdave,

I found your autocompleteTextblock very useful. But There was a problem. I had to bind Text of textbox in binding TwoWay but your given control was not able to do this. It was currenlty getting text and not setting. I have fixed this issue. If you access me at your repository then I can add my effort.

@batuypn
Copy link

batuypn commented Dec 17, 2013

I encountered same problem. Can you send me the solution?
Thank you.

@rafaqat-ali-confiz
Copy link
Author

Here is main class which I have modified.

using System;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace CustomAutoCompleteBox
{
public sealed class AutoCompleteTextBlock : Control
{
TextBox textBox = null;
ListBox listBox = null;
Grid grid = null;
public AutoCompleteTextBlock()
{
this.DefaultStyleKey = typeof(AutoCompleteTextBlock);

    }


    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        this.textBox = GetTemplateChild("tbChild") as TextBox;
        this.listBox = GetTemplateChild("lbChild") as ListBox;

        this.grid = GetTemplateChild("spContainer") as Grid;

        if (textBox != null && this.listBox != null)
        {
            textBox.TextChanged += textBox_TextChanged;
            textBox.KeyUp += textBox_KeyUp;
        }

        if (this.ItemsSource != null)
            this.listBox.ItemsSource = ItemsSource;

        if (this.Text != null)
            this.textBox.Text = Text;

        this.grid.MaxHeight = this.MaxHeight;
    }

    void textBox_KeyUp(object sender, KeyRoutedEventArgs e)
    {
        Text=this.textBox.Text;
    }



    public ObservableCollection<string> ItemsSource 
    {
        get { return (ObservableCollection<string>)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(AutoCompleteTextBlock), new PropertyMetadata(null));
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<string>), typeof(AutoCompleteTextBlock), new PropertyMetadata(null));




    void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        this.listBox.SelectionChanged -= listBox_SelectionChanged;

        this.listBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

        if (String.IsNullOrWhiteSpace(this.textBox.Text) || this.ItemsSource == null || this.ItemsSource.Count == 0)
            return;

        var sel = (from d in this.ItemsSource where d.ToUpper().StartsWith(this.textBox.Text.ToUpper()) select d);

        if (sel != null && sel.Count() > 0)
        {
            this.listBox.ItemsSource = sel;
            this.listBox.Visibility = Windows.UI.Xaml.Visibility.Visible;

            this.listBox.SelectionChanged += listBox_SelectionChanged;
        }


    }

    void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.textBox.TextChanged -= textBox_TextChanged;

        this.textBox.Text = (string)this.listBox.SelectedValue;
        Text = this.textBox.Text;

        this.textBox.TextChanged += textBox_TextChanged;

        this.listBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }

}

}

@batuypn
Copy link

batuypn commented Dec 18, 2013

Thank you so much. I wrote your class, but i could not use it. I am getting an error :
Unable to cast object of type
'project1.Common.ObservableDictionary' to type 'System.Collections.ObjectModel.ObservableCollection`1[System.String]'.
at this part of your code:
get { return (ObservableCollection)GetValue(ItemsSourceProperty); }

When i created the object in my code, i wrote:
ObservableCollectionnames = new ObservableCollection();
names.Add("sample");
this.actbNames.ItemsSource = names;

Did i use it wrong when creating?
Thank you again.

@rafaqat-ali-confiz
Copy link
Author

Yes There is a simple mistake in creating an object you have to mention its type like this
ObservableCollection names= new ObservableCollection();
names.Add("sample");
actbNames.ItemsSource = names;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants