Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wpf应用程序中使用c#使用事件处理程序生成表行焦点和松散焦点_C#_Wpf_Desktop Application - Fatal编程技术网

在wpf应用程序中使用c#使用事件处理程序生成表行焦点和松散焦点

在wpf应用程序中使用c#使用事件处理程序生成表行焦点和松散焦点,c#,wpf,desktop-application,C#,Wpf,Desktop Application,我在流程文档中放了一张表。我在后端使用c#动态地将数据添加到表中。我已经附加了事件处理程序来聚焦一行,但是如果用户单击该行以外的任何位置,我将面临移除焦点的问题 我的XAML代码如下: <FlowDocumentScrollViewer Width="auto"> <FlowDocument> <Table x:Name="feedTable">

我在流程文档中放了一张表。我在后端使用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>

是否有任何eventhadler可以用来指定从行中删除此焦点?

我不完全确定行的“焦点”是什么意思,因为您的代码所做的只是用不同的背景颜色突出显示行(当谈到GUI时,焦点的意思不同)。但是假设这就是目的,并且假设一次只能“聚焦”一行,那么有很多方法可以实现您所描述的。我不一定要这样做,但我不知道更多关于您的代码和数据,从您所提供的

首先,高亮显示指定行时,关闭所有其他行上的高亮显示

private void FocusTableRow (object sender, RoutedEventArgs e)
{
    sender.GetType ().GetProperty ("Background").SetValue (sender, Brushes.LightGray);

    foreach (var row in feedTableRowGroup.Rows) {
        if (row != sender) {
            row.GetType ().GetProperty ("Background").SetValue (row, Brushes.White);
        }
    }

    e.Handled = true;
}
我还添加了e.Handled=true来表示我们处理了鼠标按下事件并阻止其他人处理它

然后将鼠标向下处理程序添加到feedTable(在ctor中或初始化数据的位置),以便在单击行外部时捕获,并关闭“关闭”所有行高亮显示

feedTable.MouseDown += FeedTable_MouseDown;


再说一遍,这不是我应该采用的方式,也可能不是最适合WPF的方法,但它可以与您提供的方法配合使用。这听起来像是MVVM体系结构的一个很好的候选者,尽管它需要重新构造您的所有代码。

有没有一种方法可以做到这一点而不必手动取消所有其他行的高亮显示?如果您没有被锁定到FlowControlDocument类集中,您可以使用ItemsControl派生控件之一(即ListView)甚至是数据网格。所有这些都支持项目选择(多行和单行),并且可以设置为自动高亮显示。也许有一种方法可以使用TableRowGroup来做你想做的事情,但是你超出了我对流量控制的了解。感谢@Jeff花时间回答我的问题。我现在将亲自深入调查此事。
private void FocusTableRow (object sender, RoutedEventArgs e)
{
    sender.GetType ().GetProperty ("Background").SetValue (sender, Brushes.LightGray);

    foreach (var row in feedTableRowGroup.Rows) {
        if (row != sender) {
            row.GetType ().GetProperty ("Background").SetValue (row, Brushes.White);
        }
    }

    e.Handled = true;
}
feedTable.MouseDown += FeedTable_MouseDown;
private void FeedTable_MouseDown (object sender, MouseButtonEventArgs e)
{
    foreach (var row in feedTableRowGroup.Rows) {
        row.GetType ().GetProperty ("Background").SetValue (row, Brushes.White);
    }
}