如何将此集合绑定到我的列表框
我如何包装包装类的集合。我的视图模型中有一个属性,其类型
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现使用这个有效
谢谢
I found that using this works
Thanks