簡體   English   中英

使用ItemsPanelTemplate時的DisplayMemberPath

[英]DisplayMemberPath when using ItemsPanelTemplate

我有一個定義為SomeObject類型的對象的列表:

public struct SomeObject
{
  public SomeObject(int id, string imgPath)
  {
    Id = id;
    ImagePath = imgPath;
  }

  public int Id;
  public string ImagePath;
}

我想在一個列表框中顯示這些對象,該列表框為每個對象顯示一個圖像並將它們排列在圖塊布局中。 ItemsTemplate只是一個圖像。 ItemsPanelTemplate是TilePanel。

當我僅使用字符串(圖像路徑)作為列表項而不是SombeObject項目時,一切正常,並且圖像顯示正確。

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         SelectionMode="Extended"
         SelectionChanged="OnItemSelectionChanged"
         ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:VerticalTileBox}},
                    Path=SomeObjectsCollectionView, UpdateSourceTrigger=PropertyChanged}"
         >
  <ListBox.ItemTemplate>
  <DataTemplate>
      <Image Source ="{Binding}" 
                 VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Uniform" >            
      </Image>
   </DataTemplate>
  </ListBox.ItemTemplate>
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <controls:VirtualizingTilePanel ChildSize="{Binding 
        RelativeSource={RelativeSource AncestorType={x:Type controls:VerticalTileBox}},
                    Path=ItemSize, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
      <Setter Property="VerticalContentAlignment" Value="Stretch"/>
      <Setter Property="Padding" Value="0"/>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

當使用SomeObject項目時,此代碼為我提供了一個切片列表,但未顯示圖像。

System.Windows.Data錯誤:40:BindingExpression路徑錯誤:在“對象”“ SomeObject”(HashCode = 1739718653)上找不到“ ImagePath”屬性。 BindingExpression:Path = ImagePath; DataItem ='SomeObject'(HashCode = 1739718653); 目標元素是'Image'(Name =''); 目標屬性為“源”(類型為“ ImageSource”)

這是預期的,因為我沒有告訴控件確切顯示的內容(SomeObject的哪個屬性)。 除了ItemsPanelTemplate之外,還不能使用DisplayMemberPath,那么如何設置用於圖像顯示的屬性?

謝謝你的幫助!

編輯:@ nkoniishvt

這聽起來確實是正確的方法,但是我無法使其正常工作。 我究竟做錯了什么?

我將此DP添加到后面的UserControl代碼中:

public static readonly DependencyProperty ItemsTemplateProperty =
  DependencyProperty.Register("ItemsTemplate", 
  typeof(DataTemplate),
  typeof(VerticalTileBox),
  new PropertyMetadata(null));

public DataTemplate ItemsTemplate
{
  get {return (DataTemplate)GetValue(ItemsTemplateProperty);}
  set {SetValue(ItemsTemplateProperty, value);}
}

並像這樣更改了xaml:

<ListBox 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         SelectionMode="Extended"
         SelectionChanged="OnItemSelectionChanged"
         ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:VerticalTileBox}},
                    Path=ItemCollection, UpdateSourceTrigger=PropertyChanged}"
       ItemTemplate="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:VerticalTileBox}},
                    Path=ItemsTemplate, UpdateSourceTrigger=PropertyChanged}"
       >
   <ItemsPanelTemplate>
      <controls:VirtualizingTilePanel ChildSize="{Binding 
        RelativeSource={RelativeSource AncestorType={x:Type controls:VerticalTileBox}},
                    Path=ItemSize, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
  </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
      <Setter Property="VerticalContentAlignment" Value="Stretch"/>
      <Setter Property="Padding" Value="0"/>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

數據模板的定義如下:

  <DataTemplate x:Name="SomeImageItemTemplate"   DataType="utilities:SomeObject">
    <Image Source ="{Binding Path=ImagePath}" 
                 VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Uniform" />
  </DataTemplate>

在我看來,整體是這樣使用的:

  <ctrls:VerticalTileBox Source="{Binding ImageListView, UpdateSourceTrigger=PropertyChanged}" ItemsTemplate="{DynamicResource SomeImageItemTemplate}"/>

結果是顯示了圖塊,但除了對象的類名(SomeObject)之外,它們都為空。

如果作為ListBox的ItemsSource放置的數據是未知的,並且UserControl必須保持通用,則不能使用通用DataTemplate。

您應該讓UserControl的使用者提供DataTemplate,而不是在UserControl.Resources或需要創建並綁定到ListBox的ItemTemplate的DataTemplate DP中。

小組不應該說孩子的顯示方式,而應該說孩子的位置和大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM