我有一个ItemsControl
数据列表,我想虚拟化,但VirtualizingStackPanel.IsVirtualizing="True"
似乎不适用于ItemsControl
.
这是真的吗,还是有另一种方法可以做到这一点,我不知道?
要测试我一直在使用以下代码块:
<ItemsControl ItemsSource="{Binding Path=AccountViews.Tables[0]}"
VirtualizingStackPanel.IsVirtualizing="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Initialized="TextBlock_Initialized"
Margin="5,50,5,50" Text="{Binding Path=Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
如果我将其更改ItemsControl
为a ListBox
,我可以看到该Initialized
事件只运行了几次(巨大的边距只是因此我只需要通过一些记录),但是ItemsControl
每个项目都会被初始化.
我试过设置ItemsControlPanelTemplate
为a VirtualizingStackPanel
但似乎没有帮助.
我正在尝试创建一个应用选项卡,其中每个选项卡都有一个按钮区域和一个视图区域.
现在每个选项卡在布局中基本上具有相同的布局,我希望能够重用相同的布局,这样我就不必在很多地方进行更改(这只是不好的编程).我可以使用资源或样式完成此操作.
如果可能,请提供灯光代码示例.
编辑:我已经决定添加一个我正在尝试做的例子,因为我还没有得到它.
在每个TabItem下我试图重新创建这个网格(它有点复杂,但你明白了):
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Margin="10"
BorderBrush="{StaticResource MediumColorBrush}"
CornerRadius="10"
BorderThickness="2"
Grid.Row="0">
<!-- First content goes here -->
</Border>
<Border Margin="10"
BorderBrush="{StaticResource MediumColorBrush}"
CornerRadius="10"
BorderThickness="2"
Grid.Row="1">
<!-- Second content goes here -->
</Border>
</Grid>
Run Code Online (Sandbox Code Playgroud)
你也可以看到2个边框是一样的.现在我需要将一些内容占位符放在我的评论所在的位置.我不想在资源字典中声明这个Grid布局,然后在我使用它的地方将单独的内容放入每个边框.
我可能有很多TabItems,所以重复这段代码不是一个好主意,每个Tab页面将在2个占位符中有不同的内容.
我可以用了
<ContentPresenter Content="{Binding}" />
Run Code Online (Sandbox Code Playgroud)
只有1个内容的东西,当会有更多内容时会发生什么.
WPF的新功能并且具有选项卡,并且在每个选项卡中,内容以弯曲的角落面板/窗口/ whateveryouwannacallit呈现.我不确定如何做到这一点(Style,ControlTemplate)但决定采用DataTemplate方式.
所以现在我有了这个DataTemplate:
<DataTemplate x:Key="TabContentPresenter" >
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Padding="5"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
正如你可以看到的背景属性,我不想在内容中设置背景颜色,但不知道如何.我在这里使用它.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Background="White">
<!-- Something Here -->
</ContentControl>
<ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Grid.Row="1" Background="Blue">
<!-- Something Here -->
</ContentControl>
</Grid>
Run Code Online (Sandbox Code Playgroud)
在这里使用DataTemplate是错误还是有其他方法吗?
我可以直接在内容上设置背景,并从模板中的填充更改为内容中的边距,但在一些类似的情况下无法工作,只需设置一次就更好了.
编辑:
根据建议,我改为ControlTemplate并将其置于样式中.这解决了背景问题,但创造了一个更大的问题.现在内容不会出现.我在博客上看到,放一个targetType解决了这个问题,但它并没有解决我的问题.代码现在看起来像这样,并且还改变了ContentControl以使用样式而不是模板.
<Style x:Key="TabContentPresenter" TargetType="ContentControl" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Margin="10"
BorderBrush="{StaticResource DarkColorBrush}"
CornerRadius="8"
BorderThickness="2"
Grid.Row="0"
Background="{TemplateBinding Background}">
<ContentPresenter Content="{Binding}" />
</Border>
</ControlTemplate>
</Setter.Value> …
Run Code Online (Sandbox Code Playgroud)