通过编码将数据绑定到ExpanderViews

时间:2014-06-30 13:06:46

标签: c# xaml windows-phone-8 listbox windows-phone

我正在尝试从后面的代码将数据绑定到ExpanderViews。但它给我的错误和列表是空白的。屏幕上只显示左侧滑块。

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll System.Windows.Data Error: BindingExpression path error: 'groups' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='groups' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'Microsoft.Phone.Controls.ExpanderView' (Name=''); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').. System.Windows.Data Error: BindingExpression path error: 'title' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='title' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'announcementid' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='announcementid' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Tag' (type 'System.Object').. System.Windows.Data Error: BindingExpression path error: 'description' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='description' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'groups' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='groups' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'Microsoft.Phone.Controls.ExpanderView' (Name=''); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').. System.Windows.Data Error: BindingExpression path error: 'title' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='title' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'announcementid' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='announcementid' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Tag' (type 'System.Object').. System.Windows.Data Error: BindingExpression path error: 'description' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='description' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'groups' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='groups' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'Microsoft.Phone.Controls.ExpanderView' (Name=''); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').. System.Windows.Data Error: BindingExpression path error: 'title' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='title' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'announcementid' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='announcementid' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Tag' (type 'System.Object').. System.Windows.Data Error: BindingExpression path error: 'description' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='description' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availfrom' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='availfrom' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availto' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=6587942). BindingExpression: Path='availto' DataItem='MyProj.Model.MessageDTO' (HashCode=6587942); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availfrom' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='availfrom' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availto' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=37880580). BindingExpression: Path='availto' DataItem='MyProj.Model.MessageDTO' (HashCode=37880580); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availfrom' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='availfrom' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String').. System.Windows.Data Error: BindingExpression path error: 'availto' property not found on 'MyProj.Model.MessageDTO' 'MyProj.Model.MessageDTO' (HashCode=48428136). BindingExpression: Path='availto' DataItem='MyProj.Model.MessageDTO' (HashCode=48428136); target element is 'System.Windows.Documents.Run' (Name='null'); target property is 'Text' (type 'System.String')..

我正在尝试绑定MessageDTO类型的列表。 这是我的MessageDTO

public class MessageDTO
    {
        public string announcementid;
        public string lmsannouncementid;
        public string title;
        public string description;
        public string availfrom;
        public string availto;
        public string isnew;
        public string lastaccessedon;
    }

从我的代码背后,我正在做

public class Alerts
{
                private List<MessageDTO> _messageA;
                public List<MessageDTO> groups;

        private async Task getMessage()
        {

               SQLiteMessageDAO messageDAO = new SQLiteMessageDAO();
                _messageA = await messageDAO.selectMessages(idStr);
                groups = new List<MessageDTO>();

                for (int i = 0; i < _messageA.Count; i++)
                {
                    //(MessageDTO)_messageA[i]
                    MessageDTO messageDTO = new MessageDTO();
                    messageDTO.announcementid = _messageA[i].announcementid;
                    messageDTO.lmsannouncementid = _messageA[i].lmsannouncementid;
                    messageDTO.availfrom = _messageA[i].availfrom;
                    messageDTO.availto = _messageA[i].availto;
                    messageDTO.title = _messageA[i].title;
                    messageDTO.description = _messageA[i].description;
                    messageDTO.isnew = _messageA[i].isnew;
                    groups.Add(messageDTO);
                }

                if (groups != null)
                {
                    listAllAlerts.ItemsSource = groups;
                }
        }
}

数据列在清单中。在中成功添加了3个对象。 这是我的XAML

<ListBox x:Name="listAllAlerts" Margin="8,0">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>

                <toolkit:ExpanderView
                    Margin="0,0,0,8"
                    Header="{Binding }"
                    Expander="{Binding }"
                    ItemsSource="{Binding groups}">

                    <toolkit:ExpanderView.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=title}" TextWrapping="Wrap"
                                       FontFamily="Segoe" Tag="{Binding Path=announcementid}"
                                       FontSize="{StaticResource PhoneFontSizeLarge}" />
                                       <!--Foreground="{Binding Path=isnew, Converter={StaticResource alterColorConvertor}}" /-->
                        </DataTemplate>
                    </toolkit:ExpanderView.HeaderTemplate>

                    <toolkit:ExpanderView.ExpanderTemplate>
                        <DataTemplate>
                            <TextBlock Grid.Row="0" Grid.Column="1"
                                   LineHeight="25" LineStackingStrategy="BlockLineHeight"
                                   TextWrapping="Wrap">
                            <Run Text="{Binding Path=availfrom}"
                                 Foreground="{StaticResource PhoneSubtleBrush}"
                                 FontSize="{StaticResource PhoneFontSizeNormal}" 
                                 FontFamily="{StaticResource PhoneFontFamilyNormal}"/>
                            <LineBreak/>
                            <Run Text="{Binding Path=availto}"   
                                 Foreground="{StaticResource PhoneSubtleBrush}"
                                 FontSize="{StaticResource PhoneFontSizeNormal}"
                                 FontFamily="{StaticResource PhoneFontFamilyNormal}"/>
                            </TextBlock>
                        </DataTemplate>
                    </toolkit:ExpanderView.ExpanderTemplate>

                    <ListBoxItem>
                        <TextBlock Text="{Binding Path=description}" 
                                   TextWrapping="Wrap"
                                   Margin="0,0,0,-2"
                                   FontSize="{StaticResource PhoneFontSizeNormal}"
                                   FontFamily="{StaticResource PhoneFontFamilyNormal}">
                            <!--Foreground="{Binding Path=isnew, Converter={StaticResource colorConvertor}}"-->
                        </TextBlock>
                    </ListBoxItem>
                </toolkit:ExpanderView>

            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

正如我尝试过的那样解释here并且工作正常,但是当我尝试使用我的代码时,它无效。我错过了什么或者我指的是错误的价值观,有人可以指导我吗?

提前致谢。

0 个答案:

没有答案