首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemsControl在ItemsControl中

ItemsControl在ItemsControl中
EN

Stack Overflow用户
提问于 2015-09-15 13:38:13
回答 2查看 267关注 0票数 1

(连续拔头发8个小时后,我放弃了。如果有人能来救我,我会非常感激的:

我现在拥有的是:

AlbumItem:这是一个使用Canvas作为ItemsPanel并绑定到底层VM的共享属性的ItemsControl。在本控件的Resources部分中定义的少数Resources执行将ViewModel对象转换为UI对象的操作。这个控件运行得非常好,而且与预期的一样。

相册:这是一个ItemsControl,它使用UniformGrid作为其ItemsPanel,并绑定到底层VM的合谋属性。此控件显示AlbumItem的网格,我已将该控件的ItemTemplate设置为AlbumItem

不管我做什么,第二个控件不使用它的ItemTemplate来显示它的项。UniformGrid工作得很好,我得到了3x3个条目网格,但是每个条目都是一个简单的TextBlock,显示底层VM对象的类名。

我也尝试过使用DataTemplate,而不是使用ItemTemplate,但没有效果。

有人能看到哪里出了问题吗?

编辑

发布一个工作示例将是一项艰巨的任务(这是一个复杂的项目),但我可以在这里发布相关的XAML:

AlbumItem

代码语言:javascript
复制
<ItemsControl x:Class="AlbumItem" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:bd="clr-namespace:ViewProject" 
         xmlns:vm="clr-namespace:ViewModelProject" 
         mc:Ignorable="d" 
         ItemsSource="{Binding Path=Children}"
         ClipToBounds="True" Background="LightYellow">
<ItemsControl.Resources>
  <BooleanToVisibilityConverter x:Key="B2VConverter" />
  <DataTemplate DataType="{x:Type vm:Ellipse}">
    <Ellipse IsHitTestVisible="False" StrokeThickness="{Binding BorderThickness}" Fill="{Binding Fill, Mode=OneWay}" Stroke="{Binding Stroke, Mode=OneWay}" />
  </DataTemplate>  
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
   <ItemsPanelTemplate>
     <Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Focusable="true" Width="{Binding Size.Width}" Height="{Binding Size.Height}" FocusVisualStyle="{x:Null}">
     </Canvas>
   </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
 <ItemsControl.ItemContainerStyle>
    <Style>
      <Setter Property="Canvas.Left" Value="{Binding Path=Location.X}" />
      <Setter Property="Canvas.Top" Value="{Binding Path=Location.Y}" />
      <Setter Property="Panel.ZIndex" Value="{Binding Path=GlobalZOrder}" />
    </Style>
  </ItemsControl.ItemContainerStyle>
</ItemsControl>

专辑

代码语言:javascript
复制
<ItemsControl x:Class="Album"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         xmlns:bd="clr-namespace:ViewProject" 
         xmlns:vm="clr-namespace:ViewModelProject" 
         d:DesignHeight="500" d:DesignWidth="800"
         ItemsSource="{Binding Drawing.Baked}" 
         ClipToBounds="True">

  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <UniformGrid Rows="3" Columns="3" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate DataType="vm:ReadonlyDrawingVM">
      <bd:AlbumItem />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

我使用Snoop确认了以下情况:

  • 专辑的DataContext是正确的。它的ItemsSource也是如此。在深入研究ItemsSource时,我实际上可以看到Snoop中的所有VM对象。
  • 默认情况下,专辑根本不显示任何东西。如果我使用Snoop重置相册的ItemTemplate和/或ItemsPanel属性,则会看到所有项(即具有类名的TextBlock )都以网格或列表的形式出现,这取决于重置的属性。
  • AlbumItem可以很好地处理设计时的XML数据。专辑没有。
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-22 05:06:19

这最终是由于底层数据源造成的。我用一个List<T>绑定相册,由于底层属性Baked的下面一行,它没有更新UI中项的更改

代码语言:javascript
复制
If _Baked Is value Then Return

这是Baked属性设置器中的第一行(我使用的是MVVM提供的默认属性模板)。由于我从未将新的List<T>分配给_Baked;只更改了其中的项,所以上面的行将停止引发属性更改通知。一旦我修复了这个,我的专辑就开始人满为患了。

票数 0
EN

Stack Overflow用户

发布于 2015-09-15 21:27:56

我试着编写尽可能小的模仿您的代码的项目--我想我的AlbumItem VM遇到了与您和我的AlbumItem VM相同的问题。

对我来说,我正在努力在AlbumItem中使用AlbumItem,但不是通过ItemsControl.Resources直接使用。经过一些实验,看起来用于ContentPresenter的AlbumItems找不到VM的默认DataTemplate。

代码语言:javascript
复制
<ItemsControl x:Class="so_wpf_32587588.AlbumItemView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:soWpf32587588="clr-namespace:so_wpf_32587588">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas Width="100" Height="100" FocusVisualStyle="{x:Null}" IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
            <Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
            <Setter Property="Panel.ZIndex" Value="{Binding ZOrder}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="soWpf32587588:AlbumItemView">
            <Rectangle Width="40" Height="40" Fill="{Binding ItemBrush}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

在编写某种自定义ResourceDictionary时,我也遇到了类似的问题--我经历过字典已经被解析,但在ResourceDictionary使用的散列中不可用,所以任何需要该资源的控件都只能是默认的,而不是定义的资源。

您可以尝试将您的DataTemplate放在可视化(资源)树的较高位置,并查看发生了什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32587588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档