【发布时间】:2018-06-27 08:00:48
【问题描述】:
我在 FlowDocument 中放置了一个表格。我在后端使用 c# 动态地将数据添加到表中。我已附加事件处理程序以聚焦一行,但如果用户单击该行以外的任何位置,我将面临移除焦点的问题。
我的 XAML 代码是这样的:
<FlowDocumentScrollViewer Width="auto">
<FlowDocument>
<Table x:Name="feedTable">
<Table.Columns>
<TableColumn Width="*"/>
<TableColumn Width="3*"/>
<TableColumn Width="2*"/>
<TableColumn Width="4*"/>
<TableColumn Width="*"/>
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell>
<Paragraph FontSize="14pt" FontWeight="Bold">On</Paragraph>
</TableCell>
<TableCell>
<Paragraph FontSize="14pt" FontWeight="Bold">Feed Link</Paragraph>
</TableCell>
<TableCell>
<Paragraph FontSize="14pt" FontWeight="Bold">Site</Paragraph>
</TableCell>
<TableCell>
<Paragraph FontSize="14pt" FontWeight="Bold">Title</Paragraph>
</TableCell>
<TableCell>
<Paragraph FontSize="14pt" FontWeight="Bold">Feeds</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
<TableRowGroup x:Name="feedTableRowGroup">
</TableRowGroup>
</Table>
</FlowDocument>
</FlowDocumentScrollViewer>
这就是我在从 db 获取数据后将数据添加到后端表的方式
for (int i = 0; i < data.Rows.Count; i++)
{
feedTableRowGroup.Rows.Add(new TableRow());
var currentRow = feedTableRowGroup.Rows[feedTableRowGroup.Rows.Count - 1];
CheckBox checkBox = new CheckBox();
checkBox.Tag = ((int)data.Rows[i]["id"]).ToString();
checkBox.IsChecked = (byte)data.Rows[i]["is_enabled"] == 1 ? true : false;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(checkBox);
int maxChars = 25;
string link = (string)data.Rows[i]["link"];
link = link.Length <= maxChars ? link : link.Substring(0, maxChars) + "....";
currentRow.Cells.Add(new TableCell(paragraph));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(link))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run((string)data.Rows[i]["site_name"]))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run((string)data.Rows[i]["title"]))));
currentRow.Cells.Add(new TableCell(new Paragraph(new Run(((int)data.Rows[i]["items_count"]).ToString()))));
currentRow.MouseDown += new MouseButtonEventHandler(FocusTableRow);
}
当我单击“currentRow.MouseDown”时,它会将焦点添加到该行,但现在每当我单击另一个空白区域时,我都想移除此焦点。
我已经创建了用于添加和删除焦点的事件处理程序,我正在添加焦点,但现在我需要一种方法来删除这个焦点
private void FocusTableRow(object sender, RoutedEventArgs e)
{
sender.GetType().GetProperty("Background").SetValue(sender, Brushes.LightGray);
}
private void NonFocusTableRow(object sender, RoutedEventArgs e)
{
sender.GetType().GetProperty("Background").SetValue(sender, Brushes.White);
}
我可以使用任何 eventhadler 来分配以从行中移除此焦点吗?
【问题讨论】:
标签: c# wpf desktop-application