Get current Text of editable ComboBox in .NET 4 - c#

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

Related

How to update text uppercase style for Grid header in wpf C#

Need to update grid header text with Uppercase in C# WPF application
Grid header is Like: ItemName
Expected is: ITEMNAME
Please let us know how to do this?
<StackPanel>
<DataGrid
x:Name="ItemsList" AutoGenerateColumns="True"
GridLinesVisibility="None"
HorizontalAlignment="Center"
IsReadOnly="True"
BorderThickness="1"
BorderBrush="LightGray"
ColumnWidth="138.5"
ColumnHeaderStyle="{DynamicResource dataGridHeader}" />
</StackPanel>
In general, you could handle the AutoGeneratingColumn event to set the Header property of a column to a custom string, e.g.:
private void Dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Header = e.PropertyName.ToUpper();
}

System.NullReferenceException While trying to set TextBox value

I have a ComboBox in my code, I am trying to get the value of the selected item and write it in a TextBlock. while running the code i get An exception of type 'System.NullReferenceException'. What am i doing wrong?This is my Design
<StackPanel Orientation="Horizontal" >
<ComboBox SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="First" IsSelected="True"/>
<ComboBoxItem Content="Second"/>
<ComboBoxItem Content="Third"/>
</ComboBox>
<TextBlock Name="comboResult" />
</StackPanel>
and this is my code
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cb=(ComboBox)sender;
ComboBoxItem cbi = (ComboBoxItem)cb.SelectedItem;
comboResult.Text = cbi.Content.ToString();
}
Remove the IsSelected property from the ComboBoxItem and it works. You can then set ComboBox.SelectedIndex after initialization. I'm not familiar with how XAML renders, but I think that it's triggering the SelectionChanged event before the TextBlock renders. This can be observed by using a Grid with columns rather than a StackPanel, and putting the TextBlock first in the XAML (but in a higher column than the ComboBox); it works just fine. Additionally, if you catch the TextBlock's Loaded event, you can see that the SelectionChanged event is being hit first in your original code.

Disable the drop down menu of a WPF ComboBox

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.

Populate ListBox after clicking Submit button

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

Binding WPF combobox and displaying its Value to TextBox

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

Categories