Hello friends i want to display data from DB to combobox, DB table has id, investPlan, amount. Now i want to show 'investPln' col in combobox and when user selects any plan then respective amount displays in textBox control. I am able to display 'invetsPlan' rows in comboBox but don't know how to do rest thing. HELP ME!!
XAML Part
<ComboBox Height="23" Margin="70,72,88,0" Name="comboBox1" VerticalAlignment="Top" DropDownClosed="comboBox1_DropDownClosed"
ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=id}" DisplayMemberPath="fullName" SelectedValuePath="id"/>
Code Behind Part
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataSet1TableAdapters.membersTableAdapter ta = new ComboBoxDB.DataSet1TableAdapters.membersTableAdapter();
comboBox1.ItemsSource = ta.GetData();
}
You're almost there!
<TextBox Text="{Binding ElementName=comboBox1, Path=SelectedItem.amount}" />
There you go :)
Combobox has a event to fire on change of an item .You can use that .SelectionChanged event
Related
I have a ComboBox as specified below:
<ComboBox Height="31" Margin="7,7,0,0" Name="callerID" IsEditable="True" Background="LightBlue" KeyDown="callerIDbar_KeyDown" Foreground="White" FontSize="17" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0" />
storedCalls is a collection of phone numbers that will be populated to the ComboBox.Items:
foreach (string call in storedCalls)
{
if (call != "Enter a number to dial")
callerID.Items.Add(call);
}
All this works fine. I populate the Items primary because I like the autocomplete that is driven by the values in the ComboBox's Items collections. Is there a way the XAML to disable the drop down error, and disable the drop down menu? I.e. make a simple auto complete textbox like control?
I have seen full on TextBox controls that include a bunch of code-behind and complicated markup, and this is not what I am looking to do. I just need to disable the ability of the drop down menu from showing.
You can handle the DropDownOpened event and then close it.
So in n the XAML you get:
<ComboBox x:Name="cb" DropDownOpened="cb_DropDownOpened"/>
And in Code Behind:
private void cbCategoria_DropDownOpened(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.IsDropDownOpen = false;
}
I prefear this solution reather than set MaxDropDownHeight to 0.
Your choice.
in .NET 3.5 I had a implementation which gets the current edited Text of a comboBox like this:
dependencyObject.GetValue(ComboBox.TextProperty);
everything worked fine and the value was the edited text on ComboBox.Text-Property. Now we upgraded to .NET 4 and the return value is the old text and not the edited text which is the first strange behavior. But if the previous value of the ComboBox was an item from the ComboBox.ItemsSource, the code above will return the edited value. Currently I have no clue what Microsoft changed on that Property in .NET 4. Has anyone an Idea what could be different now?
Try use Text property like this:
XAML
<ComboBox Name="MyComboBox" IsEditable="True" IsTextSearchEnabled="True" SelectedIndex="0" Width="150" Height="30">
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>6</ComboBoxItem>
</ComboBox>
<Button Width="100" Height="30" Content="GetEditedText" VerticalAlignment="Bottom" Click="Button_Click" />
Code behind
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(MyComboBox.Text.ToString());
}
Or accessed through the template to the TextBox:
private void Button_Click(object sender, RoutedEventArgs e)
{
TextBox input = ((TextBox)MyComboBox.Template.FindName("PART_EditableTextBox", MyComboBox));
MessageBox.Show(input.Text.ToString());
}
I had an textbox in which user can enter text and there was an list box below the text box which shows the collections of objects of a class. When user selects any one of the list box item i am displaying the list box item in the textbox using the text property in the selected event of the list box. Now my concern here is i want to make the selected list box item as hyperlink in textbox which is clickable same as like in message composer in windows phone. And user can continue typing the text in the textbox after item was selected to select the next list box item. Can any one help me to find the solution.
yea its clear now. Can you checkthis link 'http://www.jayway.com/2011/10/05/wp7-link-in-text-with-richtextbox-on-mango/'
Hope this will help you. You can try this way.
<TextBlock x:Name="tbref" Visibility="Collapsed"/>
<RichTextBox >
<RichTextBox.Template>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<HyperlinkButton x:Name="txtnam" Content="{Binding Text,ElementName=tbref}" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox Width="200"/>
</StackPanel>
</ControlTemplate>
</RichTextBox.Template>
</RichTextBox>
selection event handler
private void lst_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
var selectedString = lst.SelectedItem;
tbref.Text = selectedString.ToString();
}
I want to populate a listbox after inputting text into a text box and clicking Submit. Seems simple I know, but I'm new to Data Binding and WPF...
Here's my code so far... I don't know if the XAML is correct, and of course I have nothing in the event code behind... any help would be appreciated.
XAML:
<ListBox ItemsSource="{Binding ElementName=accountaddTextBox, Path=SelectedItem.Content, Mode=OneWay, UpdateSourceTrigger=Explicit}" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />
Code behind:
private void okBtn_Click(object sender, RoutedEventArgs e)
{
}
Your current binding is telling the ListBox to find an object named accountaddTextBox, and bind to its SelectedItem.Content. I am assuming that accountaddTextBox is a TextBox, and SelectedItem is not a valid property on TextBox, so your binding is invalid.
It would be far better to bind your ListBox to an ObservableCollection<string> that is located in your code-behind or ViewModel, and have your button add a new object to that collection. Since it is an ObservableCollection, the UI will automatically update
For example,
<ListBox ItemsSource="{Binding SomeObservableCollection}" />
private void okBtn_Click(object sender, RoutedEventArgs e)
{
SomeObservableCollection.Add(accountaddTextBox.Text);
}
could someone help me to solve an issue with combobox behaviour. Here is my combobox control (WPF):
<ComboBox Grid.Row="1" Grid.Column="1" Margin="6,0,6,6" Name="comboBoxRegionTown" IsEditable="True" IsTextSearchEnabled="True" PreviewKeyUp="comboBoxRegionTown_PreviewKeyUp" IsTextSearchCaseSensitive="False" />
The idea is to make it autocomplete (IsEditable="True" IsTextSearchEnabled="True"). So then I typу any text into combobox it shows some results from database.
Here is a code of comboBoxRegionTown_PreviewKeyUp event (C#):
private void comboBoxRegionTown_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (!string.IsNullOrEmpty(comboBoxRegionTown.Text))
{
comboBoxRegionTown.ItemsSource = _br.GetQuery(x => x.Name.Contains(comboBoxRegionTown.Text) && x.RegionTypeId == (int)RegionType.Town).ToList();
comboBoxRegionTown.IsDropDownOpen = true;
}
else
{
comboBoxRegionTown.ItemsSource = null;
}
}
So that works fine for me, but then I click to any found item in combobox it puts into ComboBox.Text property the type of my selected object (in this case - Region). Of course I can override ToString() method for my Region object and set there its public property Name and this solution works fine, but I think the best way is to find how to bind selected item into Text property of my combobox. Is there any way to do this?
I've already tryed to ind Text="{Binding Path=Name}" and/or SelectedItem="{Binding Path=Name}" but in these cases just always get empty Text. Please help.
What you need to do is set the ItemTemplate for your ComboBox, but if you just want to display a single property there's an easier way: set DisplayMemberPath="Name" in the ComboBox and it'll generate the correct template for you.