绑定到 ListBoxItemContainerStyle 中特定集合项的属性
我有一个 Company 对象的集合,包含在 CompaniesCollection 中,该集合继承自 Observable Collection。通过 CollectionViewSource,我在列表框中显示这些公司。我的要求之一是每个公司项目必须显示每个公司所附的第一个地址(地址集合)的城市和州。
我已经尝试了一切我能想到的来展示这些价值观,但我似乎做不到。我已经验证每个 Company 对象确实至少有一个地址(大多数有两个或更多)。
这是我用来显示公司的 ListBoxItemContainer 的 ControlTemplate:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Height="Auto" Width="Auto" DataContext="{Binding Source={StaticResource CompanyDataSource}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" Background="#59000000" SnapsToDevicePixels="true" CornerRadius="10" Padding="3,1" d:LayoutOverrides="Width, Height" Grid.RowSpan="1">
<Grid Height="Auto" Width="Auto" Margin="3,1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="70"/>
<ColumnDefinition Width="*" MinWidth="30"/>
<ColumnDefinition Width="20" MinWidth="30"/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding CompanyName}" Style="{DynamicResource TitleText}" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.ColumnSpan="2"/>
<Grid Margin="0" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top">
<Ellipse Fill="#FFAB1D1D" HorizontalAlignment="Left" Height="13" Stroke="#FFDE5B5B" VerticalAlignment="Top" Width="13"/>
</Grid>
<Grid Grid.Column="1" Grid.Row="1">
<Grid Grid.Column="0" Grid.Row="0">
<TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding Addresses[0].City}" d:LayoutOverrides="Width, Height" Grid.Column="0" Grid.Row="0" Style="{DynamicResource SubtitleText}"/>
</Grid>
</Grid>
<TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding CompanyName2}" d:LayoutOverrides="Width, Height" Grid.Row="1" Style="{DynamicResource SubtitleText}"/>
</Grid>
</Border>
<Border BorderThickness="0" Grid.ColumnSpan="1" Grid.RowSpan="1" Margin="7,0" Grid.Row="1" CornerRadius="0,0,10,10" Background="#4CFFFFFF">
<ListBox Background="#00000000" ItemTemplate="{DynamicResource ContactTemplate}" ItemsSource="{Binding Contacts}"/>
</Border>
</Grid>
</ControlTemplate>
我还尝试将地址(以及与此相关的联系人)加载到 ControlTemplate.Resources 中的 CollectionViewSources 中,试图尝试访问它们,但没有成功。如何访问 DataBound ListBoxItem 中的子集合(以及该集合中的特定项目)?任何帮助都会非常有帮助。
I have a collection of Company objects contained in a CompaniesCollection which is inherited from Observable Collection. Via a CollectionViewSource, I am displaying these Companies in a list box. One of my requirements is that each company item must show the City and State of the first Address (of a collection of Addresses) attached to each Company.
I have tried everything I can think of to display those values, and I cannot seem to do it. I have verified that each Company object does indeed have at least one Address (most have two or more).
Here is the ControlTemplate for the ListBoxItemContainer I am using to display the Company:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Height="Auto" Width="Auto" DataContext="{Binding Source={StaticResource CompanyDataSource}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" Background="#59000000" SnapsToDevicePixels="true" CornerRadius="10" Padding="3,1" d:LayoutOverrides="Width, Height" Grid.RowSpan="1">
<Grid Height="Auto" Width="Auto" Margin="3,1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="70"/>
<ColumnDefinition Width="*" MinWidth="30"/>
<ColumnDefinition Width="20" MinWidth="30"/>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" Text="{Binding CompanyName}" Style="{DynamicResource TitleText}" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.ColumnSpan="2"/>
<Grid Margin="0" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top">
<Ellipse Fill="#FFAB1D1D" HorizontalAlignment="Left" Height="13" Stroke="#FFDE5B5B" VerticalAlignment="Top" Width="13"/>
</Grid>
<Grid Grid.Column="1" Grid.Row="1">
<Grid Grid.Column="0" Grid.Row="0">
<TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding Addresses[0].City}" d:LayoutOverrides="Width, Height" Grid.Column="0" Grid.Row="0" Style="{DynamicResource SubtitleText}"/>
</Grid>
</Grid>
<TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding CompanyName2}" d:LayoutOverrides="Width, Height" Grid.Row="1" Style="{DynamicResource SubtitleText}"/>
</Grid>
</Border>
<Border BorderThickness="0" Grid.ColumnSpan="1" Grid.RowSpan="1" Margin="7,0" Grid.Row="1" CornerRadius="0,0,10,10" Background="#4CFFFFFF">
<ListBox Background="#00000000" ItemTemplate="{DynamicResource ContactTemplate}" ItemsSource="{Binding Contacts}"/>
</Border>
</Grid>
</ControlTemplate>
I have also tried loading the Addresses (and Contacts for that matter) into CollectionViewSources in the ControlTemplate.Resources in an effort to try and access them to no avail. How does one access a child collection (and thereby a specific item in that collection) in a DataBound ListBoxItem? Any help would be immensely helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于保存地址集合的对象的属性是什么样的。但既然它可以说更容易,为什么不简单地向您的公司添加一个名为 FirstAddress 的属性并在 getter 中返回集合中的第一个元素呢?然后您可以绑定到 FirstAddress.City 等。当集合为空或其他任何情况时,您可以自由地返回 null,因此您不仅仅依赖于绑定。
That depends on what your object's property that holds the collection of addresses looks like. But since it would be arguably easier, why not simply add a property to your Company named FirstAddress and return the first element in the collection in the getter? Then you can bind to FirstAddress.City etc. You're free to return null when the collection is empty or anything else for that matter and so you don't depend only on the bindings.