【问题标题】:UWP XAML binding a Observable Collection class propertyUWP XAML 绑定 Observable Collection 类属性
【发布时间】:2016-04-17 11:35:17
【问题描述】:

我有一个问题 - 我想将一个作为 ObservableCollection 的类属性绑定到 GridView。

类代码:

public class Moment
{
  ...
  public ObservableCollection<Uri> PhotoFilePath { get; set; }
}



XAML

<GridView Grid.Row="0"
              Name="PhotosGridView"
              ItemsSource="{x:Bind moment}"> 

moment 是 MainPage.xaml 中 Moment 的一个实例

       <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Moment">

                <Image Width="75"  Height="75">
                    <Image.Source>
                        <BitmapImage UriSource="{x:Bind PhotoFilePath}" />

我明白了 严重性代码 描述 项目文件行抑制状态 错误无效的绑定路径“PhotoFilePath”:无法在没有转换器的情况下将类型“System.Collections.ObjectModel.ObservableCollection(System.Uri)”绑定到“System.Uri”

                    </Image.Source>
                </Image>



            </DataTemplate>
       </GridView.ItemTemplate>

    </GridView>

提前致谢

【问题讨论】:

    标签: c# wpf xaml data-binding uwp


    【解决方案1】:

    你现在拥有它的方式似乎没有意义。 momentMoment 的一个实例,而不是事物的列表(这是 ItemsSource 所期望的)。事物列表是 Moment 上的 PhotoFilePath 属性(顺便说一下,由于它是一个集合,而不是单个路径,因此命名不佳)。

    我假设您真正想做的是将ItemsSource 绑定到moment.PhotoFilePath

    ItemsSource="{x:Bind moment.PhotoFilePath}"> 
    

    您需要将 DataTemplate 更改为仅针对 Uri:

    <DataTemplate x:DataType="Uri"> 
    

    请注意,如果您可以在那里说“Uri”,我并不肯定。我面前没有 UWP 项目。

    然后将 UriSource 指向该属性内的每个项目:

    <BitmapImage UriSource="{x:Bind}" />
    

    【讨论】:

    • 感谢回复。不幸的是,仍然有问题。 Visual Studio 在该行中说 对象引用未设置为对象的实例。
    • 我意识到我遗漏了一部分答案。这有帮助吗?我在没有 UWP 项目的情况下这样做,但它应该很接近。
    【解决方案2】:

    BitmapImage 的 UriSource 属性需要类型为“System.Uri” 但您正试图将其绑定到 'ObservableCollection of System.Uri'

    (Moment 类的 PhotoFilePath 属性是 System.Uri 的 ObservableCollection)

    答案在错误信息中 '无法将类型 'System.Collections.ObjectModel.ObservableCollection(System.Uri)' 绑定到 'System.Uri'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-29
      • 2019-09-23
      • 1970-01-01
      • 2012-11-28
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多