【问题标题】:How to bind this collection to my listbox如何将此集合绑定到我的列表框
【发布时间】:2011-10-03 15:30:32
【问题描述】:

我如何包装一个包装类的集合。我的视图模型中有一个属性,其类型为

AsyncVirtualizingCollection<Item> ItemValues{get;set;}

Wrapper 是泛型类型

Wrapper<T>

物品类。基本上我正在尝试从这个link 进行数据虚拟化

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

我需要将它绑定到一个列表框(带有复选框)。我有这个模板,就这样装订

寺庙:

<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>

运行时它只显示复选框而不是标题。我知道我在绑定时做错了。

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

有什么建议可以在这里遗漏吗?

【问题讨论】:

  • 我发现使用这个工作 Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data.Caption}" 谢谢.. Shankar

标签: wpf wpf-controls


【解决方案1】:

我发现使用这个很有效

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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2010-10-10
    • 2012-02-08
    • 2012-08-23
    • 1970-01-01
    • 2011-02-16
    • 2014-03-16
    相关资源
    最近更新 更多