Combobox- Selectionchanged Event Has Old Value, Not New Value

ComboBox- SelectionChanged event has old value, not new value

According to MSDN, e.AddedItems:

Gets a list that contains the items that were selected.

So you could use:

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = (e.AddedItems[0] as ComboBoxItem).Content as string;
}

You could also use SelectedItem if you use string values for the Items from the sender:

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem as string;
}

or

private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;
}

Since both Content and SelectedItem are objects, a safer approach would be to use .ToString() instead of as string

Combobox still has old value in SelectionChanged event

The comboBox1.Text returns the value prior to the selection change, which is what is causing your problem. You can use ComboBox.SelectedItem to access the newly selected item.

The change would look something like this

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem item = comboBox1.SelectedItem as ComboBoxItem;
if (item != null)
{
switch (item.Content.ToString())
{
// ... Rest of your code here
}
}
}

However, you potentially have a problem, because the SelectionChanged event is fired when the data is loaded because you have set IsSelected="True" in you Xaml, if the textboxes are not yet created this will cause problem when the textbox values are initially set. To get around this, remove the IsSelected setting from the Xaml and just set the selection in the Load event of the Window.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.SelectedIndex = 0;
}

SelectionChanged showing the old value

You want to use the AddedItems within the EventArgs.

private void TxtProductFamily_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var addedItem in e.AddedItems)
{
var item = addedItem as String; // <-- Cast to whatever type here, string, ViewModel, int, etc.
if (item != null)
{
MessageBox.Show(item);
break;
}
}
}

What collection type are you binding to? If it's a Collection of Strings, you need to cast it first before you Show it.

ComboBox SelectionChanged event fires late

Text is just about the only property of SearchFilter that won't have been updated in your SelectionChanged handler (don't ask me why not).

SelectedItem will be good, SelectedValue will be good (in your case, both will be the selected ComboBoxItem -- not a great way to use WPF, but I'm not your priest), and SelectedIndex.

We'll make one small change to the XAML (see below) so we can get the selected string from SelectedValue.

private void SearchFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Not sure there's any reason for this null check.
if (SearchFilter.SelectedValue != null)
{
var filter = SearchFilter.SelectedValue as String;

switch (filter)
{
case "Full-Time":
EmployeeAutoBox.ItemFilter = PersonFilter_Full;
break;
case "Part-Time":
EmployeeAutoBox.ItemFilter = PersonFilter_Part;
break;
case "Retired":
EmployeeAutoBox.ItemFilter = PersonFilter_Ret;
break;
case "Stockholder":
EmployeeAutoBox.ItemFilter = PersonFilter_Stock;
break;
case "Terminated":
EmployeeAutoBox.ItemFilter = PersonFilter_Term;
break;
default:
EmployeeAutoBox.ItemFilter = PersonFilter;
break;
}
}
}

XAML: The only change aside from indenting is adding the SelectedValuePath="Content" attribute. What that does is, when the selection changes (and before the event is raised), the ComboBox will now look at the object in SelectedItem, whatever it may be, and look for a property on it named "Content". If it finds that, it'll use the value of the SelectedItem's Content property for SelectedValue. The content you're giving these is the strings: "Part-Time", etc. So then

<ComboBox 
Name="SearchFilter"
SelectedValuePath="Content"
HorizontalAlignment="Right"
MinWidth="75"
Margin="0,3,0,3"
SelectionChanged="SearchFilter_SelectionChanged"
>
<ComboBoxItem Tag="Full-Time">Full-Time</ComboBoxItem>
<ComboBoxItem>Part-Time</ComboBoxItem>
<ComboBoxItem>Retired</ComboBoxItem>
<ComboBoxItem>Stockholder</ComboBoxItem>
<ComboBoxItem>Terminated</ComboBoxItem>
<ComboBoxItem>None</ComboBoxItem>
</ComboBox>

Combo Box Selection Changed Event fires before Selection Changes when using Selected Value (C# WPF)

Use SelectionChangedEventArgs to get newly selected item

private void ListCB_Change(object sender, SelectionChangedEventArgs e)
{
var item = (KeyValuePair<int, string>)e.AddedItems[0];
MessageBox.Show("Value is now: " + item.Key);
}

WPF: why my Combobox SelectionChanged event is fired before my chart control created

You could just return from the event handler immediately if the window or the RadCartesianChart haven't yet been initialized or loaded:

private void cbInterfaces_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!this.IsLoaded || chart == null || !chart.IsLoaded)
return; //do nothing

//your code...
}

Yes but the problem is that after this form created and opened i want to see my snuff immediately in my chat instead of change my combobox selection again ...

Set the SelectedIndex property programmatically after the call to the InitializeComponent() method then:

public partial class SubForm : Window
{
public SubForm()
{
InitializeComponent();
cbInterfaces.SelectedIndex = 0;
}

private void cbInterfaces_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//...
}
}

<ComboBox
Name="cbInterfaces"
ItemsSource="{Binding Path=(local:MyClass.MachineInterfaces)}"
SelectionChanged="cbInterfaces_SelectionChanged"/>

SelectionChanged event not firing for derived ComboBox control - WPF

Good news. Issue has been identified. And the answer is, there was a PreviewMouseDown event and Focus() was called in the event. And thereby the dropdown was closed and was not available for click. I corrected the logic to solve the issue.

To identify this kind of issues, we can make use of WPF Inspector to check visual tree and Snoop which shows all events invoked. Snoop helped me to analyse the issue. Thanks for help.

Regards, ani

WPF MVVM Combobox SelectionChanged just after reload the ViewModel

You aren't calling the OnPropertyChanged() for
EmployerName,
EmpDescription,
EmpMotto,
EmpLocation,
EmpPhone,
EmpEmail

that's why your view doesn't get updated, and why are you actually using these separate properties when you can just use the ContractSelectedItem directly. You can access the nested properties like this "ContractSelectedItem.EmployerName"

Try this in your viewmodel

private ContractDetail _contractSelectedItem;
public ContractDetail ContractSelectedItem
{
get { return _contractSelectedItem; }
set
{
_contractSelectedItem = value;
OnPropertyChanged(nameof(ContractSelectedItem));
}
}

and change your view's binding like this

<StackPanel Orientation="Vertical" Grid.Row="1">
<Border Width="150" Height="150" CornerRadius="80" BorderThickness="1" BorderBrush="Gray" HorizontalAlignment="Center">
<Border.Background>
<ImageBrush ImageSource="/Assets/FEBSolution.png"/>
</Border.Background>
</Border>

<TextBlock x:Name="EmployerName" Text="{Binding ContractSelectedItem.EmployerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Margin="0 10 0 0" FontWeight="Bold"/>
<TextBlock x:Name="EmpDescription" Text="{Binding ContractSelectedItem.EmpDescription, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="11" HorizontalAlignment="Center" Opacity="0.8"/>
<TextBlock x:Name="EmpMotto" Text="{Binding ContractSelectedItem.EmpMotto, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="8" HorizontalAlignment="Center" Opacity="0.8"/>

<StackPanel Margin="20">
<StackPanel Orientation="Horizontal" Margin="0 3" HorizontalAlignment="Left">
<materialDesign:PackIcon Kind="Location" />
<TextBlock x:Name="EmpLocation" Text="{Binding ContractSelectedItem.EmpLocation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10 0"/>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0 3" HorizontalAlignment="Left">
<materialDesign:PackIcon Kind="Phone" />
<TextBlock x:Name="EmpPhone" Text="{Binding ContractSelectedItem.EmpPhone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10 0"/>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0 3" HorizontalAlignment="Left">
<materialDesign:PackIcon Kind="Email" />
<TextBlock x:Name="EmpEmail" Text="{Binding ContractSelectedItem.EmpEmail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10 0"/>
</StackPanel>
</StackPanel>
</StackPanel>

<ComboBox x:Name="cmbEmployer" Grid.Row="2" SelectedValuePath="ID" SelectedValue="{Binding ID}" ItemsSource="{Binding contractDetails}" SelectedItem="{Binding ContractSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="EmployerName" materialDesign:HintAssist.Hint="Employer" Width="200" HorizontalAlignment="Center" Margin="10">
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
<ie:InvokeCommandAction Command="{Binding SelectionChangedCommand, UpdateSourceTrigger=PropertyChanged}" CommandParameter="{Binding ElementName=cmbEmployer, Path=SelectedItem}"/>
</ie:EventTrigger>
</ie:Interaction.Triggers>
</ComboBox>

Also, there doesn't seem a reason to use the SelectionChanged event when you are just repopulating the contractDetails property every time the selection changes. Try to remove it if you aren't doing anything other then just repopulating the property.



Related Topics



Leave a reply



Submit