【问题标题】:Windows phone 8 DataTemplate in XAML not showing dataXAML 中的 Windows phone 8 DataTemplate 未显示数据
【发布时间】:2014-07-17 16:26:25
【问题描述】:

我想在我的应用程序的数据模板中显示一些信息,但是当它运行时它在列表框中没有显示任何内容。

这里是类代码(类叫notes)

public class notes
{
    public string strNoteName { get; set; }
    public string strCreated { get; set; }
    public string strModified { get; set; }
    public bool boolIsProtected { get; set; }
    public string strNoteImage { get; set; }

    public static ObservableCollection<notes> GetnotesRecord()
    {
        ObservableCollection<notes> notesRecord = new ObservableCollection<notes>  
       {  
           new notes{strNoteName="Science",strCreated="17/07/2014",strModified="17/07/2014",boolIsProtected=true,strNoteImage=""},  
           new notes{strNoteName="Math",strCreated="12/02/2014",strModified="15/07/2014",boolIsProtected=false,strNoteImage=""},  
           new notes{strNoteName="HW",strCreated="05/06/2014",strModified="2/07/2014",boolIsProtected=false,strNoteImage=""},  
           new notes{strNoteName="Business",strCreated="23/04/2014",strModified="17/07/2014",boolIsProtected=true,strNoteImage=""},

       };
        return notesRecord;
    }

    public ObservableCollection<notes> _notes;
    public ObservableCollection<notes> allNotes
    {
        get
        {
            return _notes;
        }
        set
        {
            _notes = value;

        }
    }    

}

这里是 XAML 代码:

<ListBox Margin="0,10,0,88">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Grid Margin="0,5,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto"></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding strNoteImage}" Height="80" Width="80" Grid.Column="0"></Image>
                                <StackPanel Grid.Column="1" Orientation="Vertical" Width="150" Height="100" >
                                    <TextBlock Text="{Binding strNoteName}" Margin="5,1,0,1"></TextBlock>
                                    <TextBlock Text="{Binding strCreated}" Margin="5,1,0,1"></TextBlock>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Last modified: " Margin="5,1,0,1"></TextBlock>
                                        <TextBlock Text="{Binding strModified}" Margin="3,1,0,1"></TextBlock>
                                    </StackPanel>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Is protected: " Margin="5,1,0,1"></TextBlock>
                                        <TextBlock Text="{Binding boolIsProtected}" Margin="3,1,0,1"></TextBlock>
                                    </StackPanel>
                                </StackPanel>

                            </Grid>

                        </StackPanel>


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

提前感谢您的帮助:)

【问题讨论】:

  • 首先为什么要将 boolIsProtected 绑定到 Image?
  • 对不起,我也刚刚意识到这一点。这是更新的代码@Sajeetharan

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


【解决方案1】:

将 ListBox 定义更改为

<ListBox Margin="0,10,0,88" ItemsSource="{Binding}">

并在 C# 代码中设置

this.DataContext = Notes.GetnotesRecord();

【讨论】:

    【解决方案2】:

    您缺少将 ItemsSource 设置为列表框,

    您可以在 XAML 中执行此操作并在后面的代码中设置 DataContext,就像这样,

    <ListBox Margin="0,10,0,88" Name = "lstBox1" ItemsSource= "{Binding}" />
    

    在后面的代码中,

      This.DataContext = GetnotesRecord();
    

    或者

       lstBox1.ItemsSource = GetnotesRecord();
    

    【讨论】:

    • 添加 Name = "lstBox1" ItemsSource= {Binding} 时出现错误提示“{ 不是有效名称”
    • ItemSource 应该在绑定语句周围有“”。 Like ItemsSource="{Binding}"
    • @KasunKV 感谢您的告知:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多