我已经看到一些文章,展示了如何使用AlternationIndex与ListBoxES或ListViewS,但我花了几个小时试图让基交替背景色ItemsControl类和似乎没有任何工作.ListBox我看到的所有样本都ListBoxItem用作设置背景的样式的目标类型AlternationIndex- 就像MSDN中的这样:
<Grid>
<Grid.Resources>
<Style x:Key="alternatingWithTriggers" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<Trigger Property="ListBox.AlternationIndex" Value="1">
<Setter Property="Background" Value="CornflowerBlue"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="ListBox.AlternationIndex" Value="2">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="Foreground" Value="Navy"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ListBox AlternationCount="3" ItemsSource="{StaticResource data}"
ItemContainerStyle="{StaticResource alternatingWithTriggers}">
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我想使用它,ItemsControl因为我不想要选择功能,我认为重新设置a ListBox来隐藏它可能不是最好的选择.
这是我尝试的事情之一:
<DataTemplate DataType="{x:Type vm:ObservableCollectionItem}">
<Grid>
<!-- some content here -->
</Grid>
</DataTemplate>
<!-- …Run Code Online (Sandbox Code Playgroud) 我有一个WPF应用程序,它以一个非常小的窗口开始.在200x100的区域内的东西.当从外部源引发某些事件时,我将控件添加到窗口的ItemsControl区域.items控件当前设置为使用StackPanel PanelTemplate.
我想要的是,当控件添加到itemscontrol时,应用程序窗口会增长.这是可能吗?这背后的想法是始终保持窗口尽可能小.当从itemscontrol中删除控件时,它也应该缩小.
谢谢.
我有以下(非常简单)ItemsControl:
<ItemsControl Name="BlahList" ItemsSource="{Binding Blah}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Name="MyCheckBox" Content="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
在代码中,我想做以下事情:
foreach (var dahCurrentItem in BlahList.Items)
{
var ItemCheckBox = BlahList.GimmeMyControl(dahCurrentItem, "MyCheckBox")
// I'm going to do something with the check box here...
}
Run Code Online (Sandbox Code Playgroud)
我怎么做?
如果我有一个派生的组件ItemsControl,我可以访问它的子集合,以便我可以循环它们来执行某些操作吗?我现在似乎找不到任何简单的方法.
有没有办法从ItemsControl的ItemTemplate中绑定到ItemIndex?
例如:
<ItemsControl ItemsSource="{Binding Path=ItemList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ThisItemsIndex}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个集合绑定到ItemsControl,将Canvas作为items面板,并将每个项目的Canvas.Left和Top绑定到项目对象上的属性.基本上我正在尝试重新创建我在博客上的这篇文章中描述的二维数据绑定,但这次是在WinRT而不是WPF.
由于ItemsControl将您的ItemTemplate内容包装在另一个UI元素(在WinRT的情况下为ContentPresenter)中,并且它是直接放置在items面板中的那些包装器/容器元素,因此必须在这些容器上设置Left和Top; 你不能只在DataTemplate中设置它们.在WPF中,使用ItemContainerStyle中的绑定很容易做到这一点,例如:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
但是当我在WinRT/XAML项目中尝试同样的事情时,我什么也得不到.甚至没有绑定错误.如果我对一个值进行硬编码,它就会起作用; 但是如果我使用绑定,则该属性将保持其默认值(零),并且"输出"窗口中不显示任何绑定错误.
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!-- This works, so ItemContainerStyle does work in WinRT: -->
<Setter Property="Canvas.Left" Value="200"/>
<!-- But this silently fails, leaves Top as 0, and does not show
any binding errors in the debugger's Output window: -->
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
我已经验证了ContentPresenters确实有正确的DataContext(即集合项,而不是集合本身或其他时髦的东西),所以你认为这些绑定可以正常工作.但他们似乎甚至没有得到评估.如果我在其他地方放置了一个错误的绑定,并运行调试版本,我会在调试器的Output窗口中看到绑定错误; 但是如果我在ItemContainerStyle中引用了一个无意义的属性,则不会显示绑定错误.
这是一个更完整的例子,(据我所知)应该可以在WPF中正常工作,但是在WinRT中,所有内容都保留在原点:
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style …Run Code Online (Sandbox Code Playgroud) binding itemscontrol itemcontainerstyle windows-runtime winrt-xaml
我有一个ItemsControl,其ItemTemplate DataTemplate包含一个Button.我希望按钮上的Command绑定到ItemsControl的DataContext上的Command,而不是ItemTemplate.我认为解决方案与使用RelativeSource有关,但到目前为止我的尝试都失败了:
<ItemsControl ItemsSource="{Binding Games}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=GameSelectedCommand, Source={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
CommandParameter="{Binding}"
Style="{StaticResource MenuButtonStyle}"
Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
如何让Button绑定到ItemsControl的DataContext对象的GameSelectedCommand?
嗨,我在itemscontrol的datatemplate中只有一个文本框.当我将itemcontrols绑定到一个可观察的集合时,我得到两个文本框.但我需要根据每个文本框进行一些操作,我希望使用一些id分别找到每个文本框.
任何人都可以帮助如何在WPF中的itemscontrol中找到控件.
我正在尝试编写一个简单的WPF学习项目,该项目在可调整大小的主窗口中创建一组按钮.还有就是要成为一个Button每一个集合中的条目,该集合的内容可能会在运行时有所不同.我希望按钮能够填满整个窗口(例如1个按钮@ 100%宽度,2个按钮@ 50%宽度,3个按钮@ 33%宽度等等都在100%高度).我到目前为止所编写的简化版本是:
<ItemsControl x:Name="itemscontrolButtons" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding}">
<TextBlock Text="{Binding}" />
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
...
List<string> listButtonTexts = new List<string>() { "Button1", "Button2" };
...
itemscontrolButtons.DataContext = listButtonTexts;
Run Code Online (Sandbox Code Playgroud)
这导致:

我一直无法使按钮拉伸以适应宽度,我尝试使用Grid而不是StackPanel没有结果.
然后,作为一个可选的改进,我将很感激如何调整它的建议,如果有这么多的按钮,他们不能正确地适合在一条线上或比一个阈值窄,它将包裹到一个新的线(从而减半按钮高度,如果从1到2行).
我想强调一下,我想知道如何以WPF的方式做到这一点.我意识到我可以使用窗口调整大小的消息并显式调整控件的大小,这就是我用MFC或WinForms完成它的方式,但从我读过的内容并不是如何用WPF完成的.
我正在编写WPF代码来显示实时情节,这是一条包含大约10,000点的连线.在我的电脑中显示图片大约需要5秒钟.有没有人有想法让它更快,并在0.5秒内?
class eee : FrameworkElement
{
public eee()
{
_children = new VisualCollection(this);
Random rand = new Random();
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dx = dv.RenderOpen())
{
Pen drawingPen = new Pen(Brushes.Black, 1);
double x=rand.Next(300);
double y = rand.Next(300);
for (double i = 0; i < 1000; i = i + 0.1)
{
y = 100 + rand.Next(100);
dx.DrawLine(drawingPen, new Point(i, x), new Point(i + 1, y));
x = y;
}
}
_children.Add(dv);
}
Run Code Online (Sandbox Code Playgroud) itemscontrol ×10
wpf ×9
c# ×2
data-binding ×2
xaml ×2
.net ×1
binding ×1
datatemplate ×1
findcontrol ×1
mvvm ×1
performance ×1
styles ×1
templates ×1
winrt-xaml ×1