与x的数据绑定:绑定到UWP应用程序中的列表

时间:2018-04-10 13:55:28

标签: c# data-binding uwp

UWP相当新,但不是c#并且存在数据绑定问题。 我正在尝试制作一个文件列表,我确信它很容易,但我显然遗漏了一些东西。

我有ViewModel:

namespace FileThing.ViewModels
    class FolderContext : INotifyPropertyChanged
{
    public StorageFolder SelectedFolder;
    public ICollection<StorageFile> FileList;

    public async Task<bool> GetFileList()
    {
        if (SelectedFolder != null)
        {
            // Get the file list
            FileList =  (ICollection<StorageFile>) await SelectedFolder.GetFilesAsync();
            return true; // Success..
        }
        else
        {
            return false;  // there was an error
        }

    }

  more stuff....

}

我的XAML看起来像:

<Page
x:Class="FileThing.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:FileThing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data ="using:FileThing.ViewModels"
mc:Ignorable="d" FontSize="16" >

<StackPanel>
    <StackPanel Orientation="Vertical" Margin="10,10,10,10">
        <TextBlock Name="NextStep" Text="Please Select a Folder to Process"/>
        <Button Name="FolderButton" Click="FolderButton_ClickAsync" Margin="20,20,20,20">
            <StackPanel Orientation="Horizontal">
                <TextBlock FontFamily="Segoe MDL2 Assets"  Text="&#xE8B7;"/>
                <TextBlock Text="Set Folder" />
            </StackPanel>
        </Button>
        <!--<Button Name="FolderButton" Content="Set Folder" Click="FolderButton_ClickAsync" Margin="0,0,20,0"/>-->
        <TextBlock Name="SelectedFolderPath" Text="No Folder Selected"/>
    </StackPanel>

    <ListView x:Name="FileList" IsItemClickEnabled="True" ItemsSource ="{x:Bind fc.FileList}">
        <ListViewHeaderItem>
            <TextBlock Name="Status" Text="Nothing to report so far"/>
        </ListViewHeaderItem>
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:FolderContext">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="Path" Text="{x:Bind FileList.Path}"/>
                    <TextBlock Name="FileName" Text="{x:Bind FileList.Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>


</StackPanel>

MainPage的背后代码是:

   public sealed partial class MainPage : Page
{
    FolderContext fc = new FolderContext();

    public MainPage()
    {
        this.InitializeComponent();

    }

    private async void FolderButton_ClickAsync(object sender, RoutedEventArgs e)
    {
        fc.SelectedFolder = await Helpers.GetFolder();
        SelectedFolderPath.Text = fc.SelectedFolder.Path;
        Status.Text = "Loading Files ...";
        bool filesloaded = await fc.GetFileList();
        if (!filesloaded)
        {
            NextStep.Text = "There was an Error Please try again or select another folder";
        }
        else
        {
            Status.Text = "File List Loaded successfully with " + fc.FileList.Count + " files found";
        }
    }
}

因此,用户按下“选择文件夹”按钮,代码会成功执行此操作并返回“StorageFile”列表。对象 - 我有一个单独的问题,关于StorageFiles是否有点太重了我想要的东西(简单的分析,但很多文件)... 我的错误是: 无效的绑定路径&#39; FileList.Path&#39; :Property&#39; Path&#39;无法在类型&#39; ICollection&#39;上找到FileThing 我对FileList.Name有相同的错误,这两个错误都是StorageFile对象的有效属性。引用ItemTemplate的内部TextBlock行。

我尝试过使用其他集合类型;列表,IList,ObservableCollection(首选我认为可以节省Inotifychanged骚扰?但我认为我错过了一些更基本的东西。 我确信这是一个简单的新手错误 - 任何想法......

1 个答案:

答案 0 :(得分:3)

您错误地使用了x:bind。为了更好地理解,建议您学习Data binding主题和{x:Bind} markup extension文档。

{x:Bind}使用页面或用户控件本身作为默认源。因此,它将查看页面或用户控件的代码隐藏属性,字段和方法。此外,在将{x:Bind}与数据模板一起使用时,您必须通过设置 x:DataType 值来指明要绑定的类型, DataType 应包含相应的属性,您在 DataTemplate 中绑定的字段和方法。您还可以将类型设置为接口或基类类型,然后在必要时使用强制转换来表示完整表达式。

对于您的问题,您的 x:DataType [Obsolete("IsGranted is obsolete and will be removed in a future release of the .NET Framework. Please use the PermissionSet property of either AppDomain or Assembly instead.")] 类并且StorageFile TextBlock属性可以绑定{{}应该更合适。直接1}}和Text。代码如下所示:

Path
相关问题