如何将此集合绑定到我的列表框

时间:2011-09-29 23:17:23

标签: wpf wpf-controls

如何包装包装类的集合。我的View Model中有一个属性为

的属性
AsyncVirtualizingCollection<Item> ItemValues{get;set;}

Wrapper是泛型类型

Wrapper<T>

Item类。基本上我正在尝试从这个link

进行数据虚拟化

Item是一个包含三个属性的简单类(Caption,Key和IsSelected(Implements INotifyPropertyChanged)

我需要将它绑定到列表框(带复选框)。我有这个模板并且这样绑定

Templete:

<Style x:Key="CheckBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
            <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
            <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <Border Background="{x:Null}" BorderThickness="0">
                            <ScrollViewer x:Name="scrollViewer" VerticalScrollBarVisibility="Visible"  Background="{x:Null}">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Height" Value="23" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <Grid Width="auto" x:Name="grid1">
                                        <CheckBox x:Name="checkBox" IsChecked="{Binding IsSelected}" Content="{Binding Caption}" 
                                                  HorizontalAlignment="Left" HorizontalContentAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"
                                                  Foreground="#FF3F3F3F" Background="{x:Null}"
                                                  FontSize="13" FontFamily="Segoe UI Semibold"
                                                  ClickMode="Release">
                                            <CheckBox.Style>
                                                <Style TargetType="{x:Type CheckBox}">
                                                    <Setter Property="Margin" Value="5,2,0,2" />
                                                </Style>
                                            </CheckBox.Style>
                                        </CheckBox>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

主列表框是这样的:

<Grid x:Name="gridMain" Height="auto" Width="auto" Background="White">

        <ListBox x:Name="listBox" 
                 ItemsSource="{Binding Path=ItemValues}"
                 IsSynchronizedWithCurrentItem="True"
                 Style="{StaticResource CheckBoxStyle}" 
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                 VirtualizingStackPanel.IsVirtualizing="True"  
                 Background="{x:Null}" 
                 BorderBrush="Transparent">
        </ListBox>
    </Grid>

在运行它时,它只显示Checboxes而不是Caption。我知道我在绑定方面做错了。

如果我从列表框中删除样式,它会显示字符串列表“.Wrapper`1 [.Item]”

有什么建议可以在这里找到吗?

1 个答案:

答案 0 :(得分:0)

我发现使用此功能

Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data.Caption}"

由于