【问题标题】:Bind objects in the lists below in WPF ItemsControl在 WPF ItemsControl 中绑定以下列表中的对象
【发布时间】:2016-02-25 21:32:23
【问题描述】:

我需要在 wpf 中的 ScrollViewer 中绑定 ItemsControl 中的列表对象。 我Provand分配了一条路径,但我仍然无法绑定,也许我错了?在第一级的主题中绑定是成功的,但是当我在下面的列表中同一个对象绑定将不起作用。

Xaml 滚动查看器:

<surface:SurfaceScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Hidden" Background="#fff" PanningMode="VerticalOnly">
                    <ItemsControl x:Name="scrollViewerFolderItemsSource"  ItemsSource="{Binding Path=companies}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <surface:SurfaceButton Tag="{Binding CPID}" Click="Open_Click" Grid.ColumnSpan="2">
                                        <Button.Template>
                                            <ControlTemplate TargetType="{x:Type Button}">
                                                <Border BorderThickness="0,1,0,0" BorderBrush="Gray" Height="57" Background="White">
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="80"></ColumnDefinition>
                                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                                        </Grid.ColumnDefinitions>
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition Height="60"/>
                                                        </Grid.RowDefinitions>
                                                        <Grid Grid.Row="0" Background="#fff"></Grid>
                                                        <Image Grid.Row="0" Grid.Column="0" Width="32" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding ImageFolder}"></Image>
                                                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding CompanyName}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/>
                                                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding companies.Attachments.Name}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/>
                                                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding AttachmentFolders.Name}" Foreground="#565656" FontFamily="{StaticResource Lato Semibold}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="10" Margin="10,3,0,0" Style="{DynamicResource Lato-Semibold}"/> 
                                                    </Grid>
                                                </Border>
                                            </ControlTemplate>
                                        </Button.Template>
                                    </surface:SurfaceButton>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </surface:SurfaceScrollViewer>

CodeBehind 视图列表已绑定:

我的目标是绑定 Text="{Binding Companies.Attachment.Name}"

如果我打印 Text="{Binding Attachment}" 我在部署时打印的结果是 "(Collection)”,为什么要打印 Attachment.Name

【问题讨论】:

标签: c# wpf xaml scrollviewer itemscontrol


【解决方案1】:

附件是一个集合,要可视化一个集合你应该使用一个ListBox并使用这个绑定ItemsSource="{Binding companies.Attachment}",你还需要为ListBox定义ItemTemplate。

使用 ListBox,您可以可视化所有元素,但如果您只想显示第一个附件名称,您可以使用此绑定 Text="{Binding companies.Attachment[0].Name}"

或另一种解决方案是创建一个名为 AttachmentToShow 的新属性,类型为 Attachment 并使用此绑定

Text="{Binding AttachmentToShow.Name}"

使用此解决方案更新 AttachmentToShow 将导致 UI 更新。

【讨论】:

  • 代码中有没有在[0]上添加动态索引的方法?
猜你喜欢
  • 2016-06-04
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2016-05-11
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多