WPF 组合框选择更改未正确反映到数据网格
WPF Combobox selection changed not reflected correctly to datagrid
我有一个组合框,其中包含不同版本的下拉列表。当我单击 Combobox 中的一个值时,它应该会显示该特定版本的响应数据。但是当我 运行 代码时,组合框值上的第一次点击(假设是 2019 年)没有执行任何操作。如果我点击另一个(2020),它会显示与 2019 相关的数据。DataGrid 似乎总是比 Combobox 选择晚点击一次。
这是我的代码:
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>
这是因为您在 ComboBox_SelectionChanged(..)
中调用了 grid_Construct();
,此时更改尚未应用(至少 ComboBox.Text
未应用),因此 ComboBox.Text
returns 旧值。
更正为
if (version == (dropdownList.SelectedValue as string) || (dropdownList.SelectedValue as string) == "<all>")
您应该将 version
与 ComboBox
的 SelectedItem
属性 进行比较,而不是 Text
属性:
private void grid_Construct()
{
dataGrid1.Items.Clear();
//.......
string selectedItem = dropdownList.SelectedItem as string;
foreach (string file in files)
{
//.........
if (version == selectedItem || selectedItem == "<all>")
{
dataGrid1.Items.Add(new pluginItem { addinversion = version, filepath = file });
}
if (!dropdownList.Items.Contains(version))
{
dropdownList.Items.Add(version);
}
}
}
我有一个组合框,其中包含不同版本的下拉列表。当我单击 Combobox 中的一个值时,它应该会显示该特定版本的响应数据。但是当我 运行 代码时,组合框值上的第一次点击(假设是 2019 年)没有执行任何操作。如果我点击另一个(2020),它会显示与 2019 相关的数据。DataGrid 似乎总是比 Combobox 选择晚点击一次。
这是我的代码:
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>
这是因为您在 ComboBox_SelectionChanged(..)
中调用了 grid_Construct();
,此时更改尚未应用(至少 ComboBox.Text
未应用),因此 ComboBox.Text
returns 旧值。
更正为
if (version == (dropdownList.SelectedValue as string) || (dropdownList.SelectedValue as string) == "<all>")
您应该将 version
与 ComboBox
的 SelectedItem
属性 进行比较,而不是 Text
属性:
private void grid_Construct()
{
dataGrid1.Items.Clear();
//.......
string selectedItem = dropdownList.SelectedItem as string;
foreach (string file in files)
{
//.........
if (version == selectedItem || selectedItem == "<all>")
{
dataGrid1.Items.Add(new pluginItem { addinversion = version, filepath = file });
}
if (!dropdownList.Items.Contains(version))
{
dropdownList.Items.Add(version);
}
}
}