Autocomplete textbox Linq binding
-
14 April 2012 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;
Semua Balasan
-
14 April 2012 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!
- Diedit oleh Mr. Javaman II 14 April 2012 18:00
- Diedit oleh Mr. Javaman II 14 April 2012 18:01
- Diedit oleh Mr. Javaman II 14 April 2012 18:02
-
14 April 2012 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 April 2012 4:04Autocompletebox? What version of .NET and what assembly, class is that found?
JP Cowboy Coders Unite!
-
16 April 2012 16:07Moderator
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
-
21 April 2012 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
- Ditandai sebagai Jawaban oleh Pace Nash 21 April 2012 18:23