WPF,数据网格,单击的项/行不突出显示(蓝色背景)
本文关键字:显示 蓝色 背景 网格 数据网 数据 单击 WPF | 更新日期: 2023-09-27 17:56:25
下面是 DataGrid XAML:
<DataGrid CanUserAddRows="False" AutoGenerateColumns="False" Height="501" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGridTrades" VerticalAlignment="Top" Width="574" SelectionChanged="dataGridTrades_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding TradeID}" IsReadOnly="true"/>
<DataGridTextColumn Header="Account" Binding="{Binding AccountName}" IsReadOnly="true"/>
<DataGridTextColumn Header="Card Name" Binding="{Binding CardName}" IsReadOnly="true"/>
<DataGridTextColumn Header="Set" Binding="{Binding SetMark}" IsReadOnly="true"/>
<DataGridTextColumn Header="Count" Binding="{Binding TradeCount}" IsReadOnly="true"/>
<DataGridTextColumn Header="Trade Date" Binding="{Binding TradeDate}" IsReadOnly="true"/>
<DataGridTextColumn Header="Tradepoint Name" Binding="{Binding TradepointName}" IsReadOnly="true"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}" IsReadOnly="true" Width="*"/>
</DataGrid.Columns>
</DataGrid>
下面是填充它的查询:
public static List<RowTrade> GetTradeList(DataContext dataContext)
{
return (from trade in dataContext.Trades
join account in dataContext.Accounts on trade.accountID equals account.accountID
join tradePoint in dataContext.TradePoints on trade.tradePointID equals tradePoint.tradePointID
join card in dataContext.Cards on trade.cardID equals card.cardID
join set in dataContext.Sets on card.setID equals set.setID
select new RowTrade(
trade.tradeID,
account.accountName,
card.cardName,
set.setMark,
trade.tradeCount,
trade.tradeDate.ToString(),
tradePoint.tradePointName,
(double)trade.cardPrice)).ToList();
}
在这里我填充它:
private void tabControlOrganizer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tabControlOrganizer.SelectedItem != null)
{
if (tabControlOrganizer.SelectedItem == tabItemTrades)
{
dataGridTrades.ItemsSource = Queries.GetTradeList(dataContext);
}
当我单击行时,它不会突出显示。我添加了 2 个事件来检查此行为:
private void dataGridTrades_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dataGridTrades.SelectedItem != null)
{
listBoxMessage.Items.Add(((RowTrade)dataGridTrades.SelectedItem).TradeID);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (dataGridTrades.SelectedItem != null)
{
listBoxMessage.Items.Add(((RowTrade)dataGridTrades.SelectedItem).TradeID);
}
}
当我单击(选择已更改)时,我可以在列表框上打印值。因此,所选行不为空。但是在 dataGrid 松开突出显示后,它变为空(单击按钮后我无法打印它)。
如何解决?我需要突出显示此选定项。我还有几个数据网格,并且都表现正常。
以下是有效的解决方案:
private void tabControlOrganizer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tabControlOrganizer.SelectedItem != null)
{
if (e.Source is TabControl)
{
if (tabItemTrades.IsSelected)
{
dataGridTrades.ItemsSource = Queries.GetTradeList(dataContext);
}
问题SelectionChanged
:在 C# WPF 中,为什么我的 TabControl 的 SelectionChanged 事件触发频率过高?
在空闲时间,我将检查 MVVM 来解决这个问题,ty @Mark W。