我按照这个关于如何将滚动条添加到ItemsControl的小"教程",它在Designer视图中工作,但是在我编译和执行程序时却没有(只显示前几个项目,没有滚动条可以查看更多 - 甚至当VerticalScrollbarVisibility设置为"Visible"而不是"Auto"时).
关于如何解决这个问题的任何想法?
这是我用来显示我的项目的代码(通常我使用Databinding,但是为了查看我的Designer中的项目,我手动添加它们):
<ItemsControl x:Name="itemCtrl" Style="{DynamicResource UsersControlStyle}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
<uc:UcSpeler />
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
这是我的模板:
<Style x:Key="UsersControlStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud) 我有一个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但似乎没有帮助.
我正在尝试数据绑定到这个ItemsControl:
<ItemsControl ItemsSource="{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
通过使用它DataTemplate,我试图单独定位我的Node元素Canvas正确:
<DataTemplate DataType="{x:Type Model:EndNode}">
<Controls:EndNodeControl Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,它没有按预期工作.我的所有节点元素都在相同位置绘制在彼此之上.有关如何实现这一目标的任何建议?
下面的示例使用我从代码中获取的BackupDirectories列表填充ItemsControl.
如何更改此设置以便从app.config文件中获取相同的信息?
XAML:
<Window x:Class="TestReadMultipler2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0"
Grid.Column="0"
Text="Title:"/>
<TextBlock
Grid.Row="0"
Grid.Column="1"
Text="{Binding Title}"/>
<TextBlock
Grid.Row="1"
Grid.Column="0"
Text="Backup Directories:"/>
<ItemsControl
Grid.Row="1"
Grid.Column="1"
ItemsSource="{Binding BackupDirectories}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
后台代码:
using System.Collections.Generic;
using System.Windows;
using System.Configuration;
using System.ComponentModel;
namespace TestReadMultipler2343
{
public partial class Window1 : Window, INotifyPropertyChanged
{
#region ViewModelProperty: Title
private string _title;
public string Title
{
get …Run Code Online (Sandbox Code Playgroud) 我需要在Items Control中显示一个集合中的数字列表.所以这些项目是:"1", "2", "3".
当它们被渲染时,我需要用逗号(或类似的东西)分隔它们.所以上面3个项目看起来像这样:"1, 2, 3".
如何在单个项目中添加分隔符,而不在列表末尾添加一个分隔符?
我并没有停止使用ItemsControl,但这就是我开始使用的东西.
如何以编程方式选择WPF中的项目TreeView?该ItemsControl模型似乎阻止了它.
我找不到Panel.IstItemsHost附加属性的任何好文档.我看到很多人在ItemsControl的ItemsContainer模板上设置它的例子,但在MSDN上的un-documentation并没有解释为什么或设置属性赋予什么优势.我已经构建了大量不设置此属性的容器,但尚未注意到任何不良影响.
我正在创建一个WPF表单.其中一个要求是它具有基于扇区的布局,以便可以将控件明确地放置在扇区/单元之一中.
我在下面创建了一个tic-tac-toe示例来表达我的问题:
有两种类型和一种基本类型:
public class XMoveViewModel : MoveViewModel
{
}
public class OMoveViewModel : MoveViewModel
{
}
public class MoveViewModel
{
public int Row { get; set; }
public int Column { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
表单的DataContext设置为以下实例:
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Moves = new ObservableCollection<MoveViewModel>()
{
new XMoveViewModel() { Row = 0, Column = 0 },
new OMoveViewModel() { Row = 1, Column = 0 },
new XMoveViewModel() { Row = 1, Column = 1 …Run Code Online (Sandbox Code Playgroud) 如何在ItemsControl上获得交替颜色?我将AlternationCount设置为2,但ItemsControl.AlternationIndex属性始终返回0.
<ItemsControl ItemsSource="{Binding}" AlternationCount="2">
<ItemsControl.Resources>
<Style x:Key="FooBar" TargetType="Grid">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="Blue"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,10" Style="{StaticResource FooBar}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="Auto" />
<!--<RowDefinition Height="Auto" />-->
</Grid.RowDefinitions>
<CheckBox IsChecked="{Binding Checked, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
<Label Grid.Column="1" Content="{Binding CompanyName}" />
<Label Grid.Column="2" …Run Code Online (Sandbox Code Playgroud) 仍在和WPF一起玩弄,并在我学习的同时学习.现在尝试构建一个动态的控件分组(主要是按钮,但可能包括CheckBoxes和其他).
我不知道最好的方法是什么,所以我尝试创建ItemsControl样式,然后将项目添加到WrapPanel内的ItemsPresenter中.很快意识到这些项目不会换行,因为它们实际上不在WrapPanel中,除非我把它作为ItemsHost.像这样:
<Style x:Key="ButtonPanelGroup" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border CornerRadius="5"
BorderBrush="{StaticResource DarkColorBrush}"
BorderThickness="1"
Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<WrapPanel IsItemsHost="True" FlowDirection="LeftToRight">
<ItemsPresenter />
</WrapPanel>
<ContentPresenter ContentSource="Name" Grid.Row="1" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
请注意,这是一项正在进行的工作,我仍然需要实现许多样式效果.我在这里用它:
<UniformGrid Rows="1">
<ItemsControl Name="Group1" Style="{StaticResource ButtonPanelGroup}">
<Button>Button1</Button>
<Button>Button2</Button>
<CheckBox>TickBox</CheckBox>
</ItemsControl>
<ItemsControl Name="Group2" Style="{StaticResource ButtonPanelGroup}">
<Button>Button3</Button>
<Button>Button4</Button>
<Button>Button5</Button>
</ItemsControl>
<ItemsControl Name="Group3" Style="{StaticResource ButtonPanelGroup}">
<Button>Button6</Button>
<Button>Button7</Button>
<Button>Button8</Button>
</ItemsControl>
</UniformGrid>
Run Code Online (Sandbox Code Playgroud)
另请注意,它仍然是一项正在进行中的工作,因为UniformGrid不会成为这里的方式,并且边距可能是一种痛苦(是否有任何重叠的边距?)所以输入会受到赞赏.
现在到了真正的问题.这不起作用我收到错误:
'ItemsPresenter'对象无法添加到'WrapPanel'.无法显式修改用作ItemsControl的ItemsPanel的Panel的Children集合.ItemsControl为Panel生成子元素.对象'System.Windows.Controls.ItemsPresenter'出错.
那么做这样的事情的最佳方法是什么(希望能够将按钮和其他控件扔进ItemControl并且排队真的很棒).将控件放入某种集合并迭代会更好吗?
我希望能很好地完成它,但我的WPF技能仍然缺乏.是否有任何WPF书籍超出了基础知识,并展示专业人士将如何真正做到这一点?
itemscontrol ×10
wpf ×10
c# ×4
styles ×2
wpf-controls ×2
.net ×1
app-config ×1
canvas ×1
dynamic ×1
grid ×1
itemspanel ×1
scrollviewer ×1
treeview ×1