如何仅以编程方式突出显示行 [英] How to Highlight Row programmatically only

查看:75
本文介绍了如何仅以编程方式突出显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对此处提到的代码的部分响应,该代码部分地对我有用,但并不能令人满意,不幸的是,在WPF中,我无法解决此问题并找到解决方案.因为找不到功能解决方案,所以我提出了这个问题.

This is as respond to code mentioned here which partly works for me but not as desired and unfortunately in WPF I am not able to cope with this problem and find solution. Because I was not able to find functional solution I am asking this question.

在DataGrid WPF中获取选定的行项目

试图获得结果

回复

回答了2011年4月19日22:01 经过 巴哈(Bahaa Salaheldin)

answered Apr 19 '11 at 22:01 by Bahaa Salaheldin

最有用

但是发生的是,当我向下滚动时,所选行反复出现,我认为是因为"ContainerFromItem"方法. 是否可以通过编程方式更改DataGrid的背景? -仅在C#代码中? 我试过的是,我对... selected index有点玩,所以它不像WindowsForms那样容易.因此,我试图找出如何执行突出显示". 我正在使用的是: 绑定了DataGrid项的DataObject中的DataTable是dataObject.DataTable 我知道我必须在DataGridRow上执行.Background = Brushes."DesiredColor",但是我不确定DataGrid选定索引/选定项/选定项与背景属性之间是否存在任何关系

But what happens is that while I am scrolling down , selected line is repeatedly showing up , I think because of "ContainerFromItem" approach. Is it possible to change background of DataGrid programmatically ? - only in C# code ? What I tried was that I played a little with ... selected index and so and it is not as easy as it in WindowsForms is . So I tried to find out how to perform Highlighting . What I am using are: DataTable in DataObject that is binded DataGrid Items Soruce is dataObject.DataTable I know that I have to perform .Background = Brushes."DesiredColor" on DataGridRow But I am not sure if there is any relation between DataGrid Selected Index / Selected Item / SelectedItems and Background property

19/06/19添加了更多信息:

我在这里发布示例代码,但是此代码可用于Windows窗体, 我以为我可以做这样的事情.

Here I am posting example code , but this code is functional for windows forms , I thought I can do something like this.

foreach (DataGridViewRow row in dgvNetlist.Rows)
{
    if (row.Cells[2].Value.ToString().Contains(Messages.SingleConnection))
        row.DefaultCellStyle.BackColor = databaseColor[0];                     //error color                    
    else if (row.Cells[2].Value.ToString().Contains(Messages.MissingTP))
        row.DefaultCellStyle.BackColor = databaseColor[1];                    
    else if (row.Cells[2].Value.ToString().Contains(Messages.MissingConnection))//if message cell contains missing connections
        row.DefaultCellStyle.BackColor = databaseColor[2];                    
    else if (row.Cells[2].Value.ToString().Contains(Messages.MultipleTPs) && cbHideMultipleTPs.Checked == false)  //if message cell contains multiple TPs
        row.DefaultCellStyle.BackColor = databaseColor[3];
    else if (row.Cells[2].Value.ToString().Contains(Messages.EmptyNet))      //if message cell contains Empty net
        row.DefaultCellStyle.BackColor = databaseColor[0]; 
    else
        row.DefaultCellStyle.BackColor = databaseColor[4];                  //OK color

    if (row.Cells[4].Value.ToString().Equals("True"))                       //if row is marked -> marked color
        row.DefaultCellStyle.BackColor = databaseColor[5];
    i++;
}

但是,例如,使用自己的规则,用户必须保存"某行,他想突出显示该行. 我要做的是分步进行:

But with own rules, for example, user has to "save" some row, he wants to highlight the row. What I want to do is in steps:

1)从选定的ROW中获取项目ID ...项目ID我的意思是表中的原始ID,例如ID号950
2)做一些突出的动作,例如.不同颜色的背景.
3)将ID保存到用户设置中,因为每个用户可以在不同的行中突出显示

1) Get the item ID from selected ROW ... Item ID I mean the original ID in table for instance ID number 950
2) do some highlight action , eg. background to different colour.
3) save the ID to the user settings because each user can have different rows highlighted

4)在另一个应用程序启动时,使用一些周期来查找已保存的行(数据库中是否有更多或更少的项目都无关紧要)并突出显示行-不管它们的顺序如何-突出显示取决于项目ID. ..

4) on another application start, use some cycle to find saved rows (does not matter if there are more or less items in database) and highlight the rows - does not matter how ordered they are - highlighting depends on item ID ...

我认为可以从datagrid->选定项目中获取ID

I thought it is somehow possible while I can get ID from datagrid-> selected item

但是我找不到像上面发布的代码那样的方法.

but I found no way to do it like in code posted above.

推荐答案

由于您没有发布任何代码,因此很难说出您到底想要什么.要更改背景颜色,请使用绑定到IsSelected的DataTrigger;如果为true,则将背景颜色设置为所需的突出显示颜色

Since you don't have any code posted its difficult to tell what exactly you want. To change the background colour use a DataTrigger bound to IsSelected and if it is true then set the background colour to your desired highlighted colour

    <Style TargetType="{x:Type ContentControl}">
        <Setter Property="Visibility" Value="Collapsed"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

更新:如果要以编程方式进行操作,则可以在代码中创建绑定,其中Listboxitem是包含该项的列表框,SelectedToBackgroundConverter是实现您在您的文章中提到的规则的转换器帖子.

UPDATE: If you want to do it programatically then you can create the binding in the code, where Listboxitem is the listbox containing the item and SelectedToBackgroundConverter is a converter which implements the rules you mentioned in your post.

        gridLine.SetBinding(BackgroundProperty, new Binding(nameof(IsSelected)) { Source = ListBoxItem, Converter = new SelectedToBackgroundConverter() });

尽管在代码中执行此操作比使用xaml要复杂得多.

Doing this in code though is much more complicated than using xaml.

这篇关于如何仅以编程方式突出显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆