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

发布于 2024-12-07 13:48:43 字数 3998 浏览 1 评论 0原文

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

AsyncVirtualizingCollection<Item> ItemValues{get;set;}

通用类型

Wrapper<T>

为 Wrapper 是Item 类的 。基本上我试图从这个 link 进行数据虚拟化

Item 是一个简单的类,包含三个属性(Caption、Key 和 IsSelected(实现 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>

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

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

有什么建议吗?这里可能缺少什么?

How do i wrap a collection of wrapped class. I have a property in my View Model which is of type

AsyncVirtualizingCollection<Item> ItemValues{get;set;}

Where Wrapper is a generic type

Wrapper<T>

Of Item class. Basically I am trying to do a data Virtualization from this link

Item is a simple class containing three properties (Caption, Key and IsSelected (Implements INotifyPropertyChanged)

I need to bind that to a List Box (with checkboxes). I have this templete and am binding this way

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>

And the Main Listbox is like this :

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

On running this it only shows the Checboxes and not the Caption. I know i am doing something wrong here in binding.

If i remove the style from the list box it shows me list of strings ".Wrapper`1[.Item]"

Any suggestions what could be missing here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-14 13:48:43

我发现使用这个有效

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

谢谢

I found that using this works

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

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文