Answered Autocomplete textbox Linq binding

  • 14 เมษายน 2555 17:35
     
     

     Hello,

    I'm a beginner.

    Please help I am trying to add an ID t.DiagnosisID in the Autocomplete TextBox so that when I click enter I can get the ID instead of the Name e.g.  t.Description. How do I see the autocomplete text t.Description without the t.DiagnosisID but when I hit enter I get the t.DiagnosisID. Here is the code I currently have:

    var Group = from b in conn.tblDiagnosis
                            where b.IsActive == true
                            orderby b.Description
                            select b;
                List<string> diagnosis = new List<string>();
                diagnosis.Clear();
                foreach (var t in Group)
                {
                    diagnosis.Add(t.Description);
                }
                cbDiagnosis.ItemsSource = diagnosis;

ตอบทั้งหมด

  • 14 เมษายน 2555 17:59
     
      มีโค้ด

    Collections in WPF are powerful because you can pick any field within them to show in the GUI!

    So, your Group collection above, is an IENUMERBALE collection of something with lots of fields.

    In WPF where you have the CBDiagnosis and where you set the ItemSource, you can speicify the PATH.  The Path then is a field name within the collection of Group.  So the visible content the user sees will use the path of  ID.

    So the first thing you have to learn is how to bind a collection to a CB control using WPF.  Once you do that, your code above will change what is shown here:

    var Group = from b in conn.tblDiagnosis
                             where b.IsActive == true
                             orderby b.Description
                             select b;
                 List<string> diagnosis = new List<string>();
                 cbDiagnosis.ItemsSource = diagnosis;

    <ComboBox x:Name="cbDiagnosis"

    DisplayMemberPath="ID"

    SelectedValuePath="SomeOtherField" />



    JP Cowboy Coders Unite!




    • แก้ไขโดย Mr. Javaman II 14 เมษายน 2555 18:02
    •  
  • 14 เมษายน 2555 18:18
     
     

    Sorry

    May be I got you wrong my Xaml is ont a ComboBox.

    Xaml:

     <my:AutoCompleteBox HorizontalAlignment="Left" KeyUp="cbDiagnosis_KeyUp" Margin="82,280,0,0" Name="cbDiagnosis" VerticalAlignment="Top" Width="520" />

  • 16 เมษายน 2555 4:04
     
     
    Autocompletebox?  What version of .NET and what assembly, class is that found?

    JP Cowboy Coders Unite!

  • 16 เมษายน 2555 16:07
    ผู้ดูแล
     
      มีโค้ด

     How do I see the autocomplete text t.Description without the t.DiagnosisID but when I hit enter I get the t.DiagnosisID.

    Hi Pace,

    When you hit enter, the Text in AutoCompleteBox is the selectedItem's Description property, right? You can just get SelectedItem first, then to get what you want in this simple way, please check out below codes for your reference:

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            private void myCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Hall hal = (Hall)myCB.SelectedItem;
                Console.WriteLine(hal.B);
            }
        }
        public class Hall:INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
            private string description;
            private string id;
            public string Description
            {
                get
                {
                    return this.description;
                }
                set
                {
                    if (value != this.description)
                    {
                        this.description = value;
                        NotifyPropertyChanged("Description");
                    }
                }
            }
            public string Id
            {
                get
                {
                    return this.id;
                }
                set
                {
                    if (value != this.id)
                    {
                        this.id = value;
                        NotifyPropertyChanged("Id");
                    }
                }
            }
            public Hall(string a, string b)
            {
                this.Description = a;
                this.Id = b;
            }
        }
        public class NameList : ObservableCollection<Hall>
        {
            public NameList()
                : base()
            {
                Add(new Hall("Willa", "1"));
                Add(new Hall("Isak", "2"));
                Add(new Hall("Victor", "3"));
                Add(new Hall("Jules", "4"));
            }
        }

    XAML:

    <Window.Resources>    
            <local:NameList x:Key="mylist"/>
    </Window.Resources>
        <Grid>
           
             <StackPanel>
                <ComboBox Name="myCB" ItemsSource="{StaticResource mylist}" SelectionChanged="myCB_SelectionChanged">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=A}" Background="Tomato"/>
                        </DataTemplate>
                        
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </StackPanel>
        </Grid>

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    • ทำเครื่องหมายเป็นคำตอบโดย Pace Nash 21 เมษายน 2555 17:56
    • ยกเลิกการทำเครื่องหมายเป็นคำตอบโดย Pace Nash 21 เมษายน 2555 18:16
    •  
  • 21 เมษายน 2555 18:22
     
     คำตอบ

    I decided to detect what is inside the box and made it unique. Check the description and get the Diagnosis.

    thankyou for your responses

    • ทำเครื่องหมายเป็นคำตอบโดย Pace Nash 21 เมษายน 2555 18:23
    •