0

我的DataGrid应用程序中有几列。在某些特定字段内容的情况下,我需要使用不同的行前景色。LoadingRow我对事件使用以下回调:

 void userGrid_LoadingRow(object sender, DataGridRowEventArgs e){

            if (e.Row.Item != null){  
                // check some row's field value               
                ...
                // modify row forecolor
                e.Row.Foreground = new SolidColorBrush(Colors.Red);
                ...
            }
        }

但是如何通过名称或索引获取某些行的字段的值?

4

1 回答 1

1
   void userGrid_LoadingRow(object sender, DataGridRowEventArgs e)
   {
        var dataGrid = (DataGrid)sender;
        IEnumrable<DataGridRow> rows = dataGrid.Items.Where(r =>  (r.DataContext as YourItemsSourceEntity).SomeProperty == yourCondition)
   }

或者,我会在您的 ItemsSource 中添加一个条件。

        public class YourItemsSourceEntity
        {
             public bool IsSomething { get; }
        } 

xml:

     <DataGrid ItemsSource="{Binding Items}">
        <DataGrid.ItemContainerStyle>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSomething}" Value="True">
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.ItemContainerStyle>                
    </DataGrid>          

至于下面的评论:这是你的意思吗?

    void userGrid_LoadingRow(object sender, DataGridRowEventArgs e)
   {
        var item = e.Row.DataContext as (YourItemsSourceEntity);
        var id = item.ID ;   
   }
于 2013-10-31T12:58:24.663 回答