Grid没有为两个表中的列显示某些值

时间:2013-06-06 15:31:35

标签: wpf linq

这里我有一个填充消息字段的方法

   public List<MessageFieldViewModel> GetAllViewModelMsgFields()
    {
        messageFieldVModel = messageField.GetAllMessageField().Select(msgFields => new MessageFieldViewModel
        {
            Id = msgFields.Id,
            Code = msgFields.Code,
            Name = msgFields.Name,
            Position = msgFields.Position,
            Length = msgFields.Length,
            IsMapped = (transactionRuleList.Any(tr => tr.SourceElementId == msgFields.Id)),
            MappingRule = transactionRuleList.Any(mapRule => mapRule.SourceElementId 
                                                             == msgFields.Id)?

                          transactionRuleList.First(mapRule => mapRule.SourceElementId 
                                                               == msgFields.Id).MappingRule

                          : null
        })
    .ToList();
        return messageFieldVModel;
    }

在我的网格上,我想显示所有值:

  <DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2"    AutoGenerateColumns="False"  IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding Id}" />
                <DataGridTextColumn Header="Code" Binding="{Binding Code}" />
                <DataGridTextColumn Header="Field Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Position" Binding="{Binding Position}" />
                <DataGridTextColumn Header="Length" Binding="{Binding Length}" />
                <DataGridTemplateColumn Header="IsMapped">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Path=IsMapped}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="MappingRule" Binding="{Binding MappingRule}" /> 
            </DataGrid.Columns>
        </DataGrid>

//一些MessageField数据

        MessageField.Add(new MessageFieldModel(01, "GMSLEN", "MESSAGE LENGTH", 5, 4));
        MessageField.Add(new MessageFieldModel(01, "GMSDST", "MESSAGE DESTINATION", 9, 7));
        MessageField.Add(new MessageFieldModel(011, "GMSOR", "MESSAGE ORIGIN", 16, 7));

        MessageField.Add(new MessageFieldModel(02, "GMSLEN", "MESSAGE LENGTH", 5, 4));
        MessageField.Add(new MessageFieldModel(02, "GMSDST", "MESSAGE DESTINATION", 9, 7));
        MessageField.Add(new MessageFieldModel(012, "GMSOR", "MESSAGE ORIGIN", 16, 7));

//一些翻译规则数据

        TranslationRule.Add(new TranslationRuleModel(01, 01, 690, "direct"));
        TranslationRule.Add(new TranslationRuleModel(01, 01, 690, null));

        TranslationRule.Add(new TranslationRuleModel(02, 02, 690, "direct"));
        TranslationRule.Add(new TranslationRuleModel(02, 02, 690, null));

        TranslationRule.Add(new TranslationRuleModel(03, 03, 690, "direct"));
        TranslationRule.Add(new TranslationRuleModel(03, 03, 690, null));

现在我的网格显示了IsMapped的值,但是对于MappingRule,我想同时看到direct和null。目前它不显示null。有人能帮我理解我做错了吗?

2 个答案:

答案 0 :(得分:1)

我刚刚发现了查询的错误 这就是我想要做的事情

    MappingRule = transactionRuleList.Any(mapRule => mapRule.SourceElementId == msgFields.Id)
                          ?transactionRuleList.First(mapRule => mapRule.SourceElementId == msgFields.Id).MappingRule:"NULL"

答案 1 :(得分:0)

好的,现在我想我明白了。 MappingRule类声明是什么样的?如果要在MappingRule中显示“direct”或“null”,则必须将绑定挂钩到存储它的属性。

例如,如果“direct”存储在MappingRule的Type属性中,则绑定必须在MappingRule.Type上,而不是MappingRule。

也许这有帮助?