DataGrid LoadingRow事件问题 [英] DataGrid LoadingRow event problem

查看:193
本文介绍了DataGrid LoadingRow事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用datagrid loadingrow事件根据行中的值为行着色.我在loadingrow事件处理程序中有以下代码:

I''m trying to use the datagrid loadingrow event to color my row based on the value within the row. I have the following code within the loadingrow event handler:

<br />
DataRowView item = e.Row.Item as DataRowView;<br />
if (item != null)<br />
{<br />
   // my code to set the row background color<br />
}<br />


我的问题是变量项始终为null,因此我的代码永远不会执行.我使用了调试器,当我查看"e.Row.Item"值时,它的确显示了除"null"之外的内容.因此,我不知该如何导致赋值导致空"而不是我看到的"e.Row.Item"值.
任何人都可以阐明问题并提供解决方案吗?
我已插入"Console.WriteLine(e.Row.Item);"紧接在上述"if"语句之后的语句,它确实显示一个值,而不是null!
预先感谢您的帮助.


My problem is that the variable item is always null so my code never gets executed. I''ve used the debugger and when I view the "e.Row.Item" value, it does indeed show something besides "null." So, I''m at a loss for what would cause the assignment to result in "null" instead of the value I see as "e.Row.Item."
Can anyone shed any light on the problem and offer a solution?
I''ve inserted a "Console.WriteLine(e.Row.Item);" statement right after the above "if" statement and it indeed displays a value, not null!
Thanks in advance for the help.

推荐答案

因此,对这只小狗又做了一次更新,目的是让人们知道我发现的新信息.之前在我的LoadingRow事件处理程序中,我正在像这样设置变量:
DataRowView item = e.Row.Item作为DataRowView;

然后检查它的值,只是发现项目的值从未更新过...
我在Internet上进行了更多搜索,找到了另一个在LoadingRow事件处理程序中起作用的解决方案"(除了我先前发布的XAML触发解决方案之外).因此,这是新的" LoadingRow事件处理程序代码:
So, one more update to this puppy just to let folks know about new information that I found. In my LoadingRow event handler previously, I was setting the variable like this:
DataRowView item = e.Row.Item as DataRowView;

and then checking it''s value, only to find that the value of item was never getting updated...
I did some more searching on the internet and found another "solution" that works in the LoadingRow event handler (in addition to the XAML trigger solution I previously posted). So, here''s the "new" LoadingRow event handler code:
<br />
<pre lang="cs">void dataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)<br />
{<br />
    // Get the DataRow corresponding to the DataGridRow that is loading...<br />
    OptionChain RowDataContext = e.Row.DataContext as OptionChain;<br />
<br />
    if (RowDataContext != null)<br />
    {<br />
        if (RowDataContext.ochgdir == "Down")<br />
            e.Row.Background = Brushes.Beige as Brush;<br />
        else<br />
            e.Row.Background = Brushes.GreenYellow as Brush;<br />
    }<br />
}</pre><br />
<br />


请注意,新代码中的区别在于,我创建了RowDataContext变量而不是DataRowView变量.生成的代码更简单,更有效! DataGrid绑定到"OptionChain"对象的可观察集合,而ochgdir是每个对象的属性.
我想知道原始版本的问题可能与单向绑定还是双向绑定有关,实际上这不允许在我的原始LoadingRow事件处理程序中进行赋值吗?如果不是这样,哦,那还是个谜!
谢谢.


Note the difference in the new code is that I create a RowDataContext variable rather than a DataRowView variable. The resulting code is a bit simpler and IT WORKS! The DataGrid is bound to an observablecollection of "OptionChain" objects and ochgdir is a property of each of those objects.
I''m wondering if the problem with the original version might have something to do with one-way vs. two-way binding which, in effect, doesn''t allow the assignment in my original LoadingRow event handler? If that''s not the case, oh well, it''s still a mystery!
Thanks.


发生此问题可能是因为LoadingRow事件实际上可能触发了几次. Datagrid仅在需要时实例化该行,而在其不在视图范围内时将对其进行回收.就像开始滚动网格时一样.

有关详细信息,请参见备注部分: http: //msdn.microsoft.com/zh-cn/library/system.windows.controls.datagrid.loadingrow(v=vs.95).aspx [
The problem occurs probably because the LoadingRow event may actually fire several times. The Datagrid will only instantiate the row when it is needed and will recycle it when it goes out of view. Like when you start scrolling the grid.

See the remarks sections for details: http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.loadingrow(v=vs.95).aspx[^]


好吧,我实际上并没有解决为什么我的LoadingRow事件处理程序中没有发生分配的问题,那仍然是一个 MYSTERY ;但我确实实现了我想要的另一种方式.我修改了XAML,使其包含以下代码:
Well, I didn''t actually solve the problem of why the assignment wasn''t happening in my LoadingRow event handler, that''s still a MYSTERY; but I did accomplish what I wanted another way. I modified my XAML to include the following code:
<br />
<pre lang="xml"><DataGrid.RowStyle><br />
     <Style TargetType="DataGridRow"><br />
         <Setter Property="TextElement.Foreground" Value="Green"/><br />
         <Style.Triggers><br />
             <DataTrigger Binding="{Binding ochgdir}" Value="Down"><br />
                 <Setter Property="TextElement.Foreground" Value="Red"/><br />
             </DataTrigger><br />
         </Style.Triggers><br />
     </Style><br />
 </DataGrid.RowStyle></pre><br />


结果是,当绑定到DataGridRow的对象的"ochgdir"属性包含字符"Down"时,则DataGrid行数据的前景色将设置为"Red",否则将其设置为"Green". br/> 编程"的奇妙之处在于,对于一​​个问题,总会有不止一种解决方案……这仅仅是找到适合您的解决方案!我DID!
感谢那些考虑我的帖子并感谢Ganesan的回复.


The result is that when the "ochgdir" property of the object bound to the DataGridRow contains the characters "Down" then the foreground color of the DataGrid row data is set to "Red" otherwise it''s set to "Green."
Wonderful aspect of "programming" is that there''s always more than one solution to a problem...it''s just a matter of finding one that works for you! I DID!
Thanks to those who''ve pondered my post and to Ganesan for the reply.


这篇关于DataGrid LoadingRow事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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