I have a Combobox that has a dropdown list of different versions. And when I clicked one of the values in the Combobox, it supposes to show the responding data for this specific version. But when I run the code, the first click (let's said 2019) on Combobox value is not doing anything. If I click the other one (2020), it shows the data related to 2019. DataGrid seems always one click later than Combobox selection.
Here is my code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dropdownList.Items.Add("<all>");
dropdownList.SelectedIndex = 0;
grid_Construct();
}
private void grid_Construct()
{
dataGrid1.Items.Clear();
.......
foreach (string file in files)
{
.........
if (version == dropdownList.Text || dropdownList.Text == "<all>")
{
dataGrid1.Items.Add(new pluginItem { addinversion = version, filepath = file });
}
if (!dropdownList.Items.Contains(version))
{
dropdownList.Items.Add(version);
}
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
grid_Construct();
}
public class pluginItem
{
public string addinversion { get; set; }
public string filepath { get; set; }
}
}
XAML:
<Grid Grid.ColumnSpan="2" Height="34" VerticalAlignment="Top" Background="#FF9C9B9B">
<Button x:Name="btnAbout" Content="About" Margin="0,6,24,7" Click="BnAbout_Click" RenderTransformOrigin="1.229,0.459" HorizontalAlignment="Right" Width="58"/>
<Label Content="Revit Version : " HorizontalAlignment="Left" Height="27" Margin="12,2,0,0" VerticalAlignment="Top" Width="140"/>
<ComboBox x:Name="dropdownList" HorizontalAlignment="Left" Height="21" Margin="101,6,0,0"
VerticalAlignment="Top" Width="178" SelectionChanged="ComboBox_SelectionChanged" SelectedIndex="0"
ItemsSource="{Binding Path=pluginItem}"/>
</Grid>
<DataGrid x:Name="dataGrid1" Grid.ColumnSpan="2" Margin="15,54,15,51" Background="White"
HeadersVisibility="Column" MinColumnWidth="5" RowHeight="22" CanUserReorderColumns="False"
AreRowDetailsFrozen="True" RowDetailsVisibilityMode="Visible" CanUserAddRows="False"
SelectionChanged="dataGrid1_SelectionChanged" ItemsSource="{Binding Path=pluginItem}" VirtualizingStackPanel.IsVirtualizing="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Version" Width="118*" Binding="{Binding addinversion}" IsReadOnly="True"/>
<DataGridTextColumn Header="Addin Path" Width="440*" Binding="{Binding filepath}" IsReadOnly="True"/>
</DataGrid.Columns>
This is because you call grid_Construct();
in ComboBox_SelectionChanged(..)
and at this point the change is not applied yet(at least by the ComboBox.Text
), so the ComboBox.Text
returns the old value.
Correction would be
if (version == (dropdownList.SelectedValue as string) || (dropdownList.SelectedValue as string) == "<all>")