- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Rachel Lim 的 GridHelper获取动态行数。我想要实现的是让每一行显示在另一行下方(完成),能够调整它们的大小(完成 - 使用 GridSplitter)并根据屏幕大小按比例调整内容。
结果:
Xaml:
<Grid>
<ItemsControl ItemsSource="{Binding RowSource}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid local:GridHelper.RowCount="{Binding RowCount}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="{Binding RowNumber}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Col 1" />
<DataGridTextColumn Header="Col 2" />
<DataGridTextColumn Header="Col 3" />
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="1" Content="Btn" />
</Grid>
<GridSplitter Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Grid.Row="0" ResizeDirection="Rows" ResizeBehavior="CurrentAndNext"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
View 模型:
internal class MyViewModel
{
public ObservableCollection<RowInfo> RowSource { get; set; }
public int RowCount { get { return RowSource.Count; } }
public MyViewModel()
{
RowSource = new ObservableCollection<RowInfo>()
{
new RowInfo() { RowNumber = 0 },
new RowInfo() { RowNumber = 1 },
new RowInfo() { RowNumber = 2 }
};
}
}
行信息:
public class RowInfo
{
public int RowNumber { get; internal set; }
}
最佳答案
我认为你的做法是错误的。您不能使用 ItemsControl
,因为 GridSplitter
项目需要位于 ItemsPanel
级别而不是 DataTemplate
- 否则,它将不起作用。
您最好在网格本身上使用自定义行为 - 请参阅下面的示例代码:
public class GridAutoRowChildBehavior : Behavior<Grid>
{
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(GridAutoRowChildBehavior),
new PropertyMetadata(null, OnGridPropertyChanged));
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(object), typeof(GridAutoRowChildBehavior),
new PropertyMetadata(null, OnGridPropertyChanged));
private static void OnGridPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((GridAutoRowChildBehavior) d).ResetGrid();
}
private void ResetGrid()
{
var source = ItemsSource as IEnumerable;
if (source == null || ItemTemplate == null)
return;
AssociatedObject.Children.Clear();
AssociatedObject.RowDefinitions.Clear();
var count = 0;
foreach (var item in source)
{
var content = new ContentPresenter
{
ContentTemplate = ItemTemplate,
Content = item
};
var splitter = new GridSplitter
{
Height = 5,
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Stretch
};
AssociatedObject.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
Grid.SetRow(content,count);
Grid.SetRow(splitter,count);
AssociatedObject.Children.Add(content);
AssociatedObject.Children.Add(splitter);
count++;
}
}
public DataTemplate ItemTemplate
{
get { return (DataTemplate) GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public object ItemsSource
{
get { return GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
}
然后在您的 XAML 中像这样编码:
<Grid>
<i:Interaction.Behaviors>
<local:GridAutoRowChildBehavior ItemsSource="{Binding RowsSource}">
<local:GridAutoRowChildBehavior.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Col 1" />
<DataGridTextColumn Header="Col 2" />
<DataGridTextColumn Header="Col 3" />
</DataGrid.Columns>
</DataGrid>
<Button Grid.Column="1" Content="Btn" />
</Grid>
</DataTemplate>
</local:GridAutoRowChildBehavior.ItemTemplate>
</local:GridAutoRowChildBehavior>
</i:Interaction.Behaviors>
</Grid>
我已经对此进行了测试,它完全符合您的需要。
您唯一需要做的就是将 Nuget 包 Systems.Windows.Interactivity.WPF
添加到您的项目
关于wpf - ItemsControl - 网格子元素自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46705206/
我正在寻找的服务器是: 轻量级, 非 buggy , 支持.NET, 在客户端上运行以进行测试, 在Windows上运行 Cassinni太过马车,IIS太昂贵,Apache很难安装,XSP仅是lin
所以我有大约10个短的css文件,可以与mvc应用程序一起使用。 有像 error.css login.css 等等... 仅有一些非常短的CSS文件,这些文件使更新和编辑变得容易(至少对我而言)。我
我正在编写程序来自动化 win32 表单。我正在使用 Microsoft UI 自动化库。我不知道如何获取和调用该表单上的预定义快捷键。现在我只需获取 MenuItem 的 AutomationEle
我有一个在后台线程上运行的及时操作。运行时,我当前将光标置于等待状态: Mouse.OverrideCursor = Cursors.Wait 我刚刚实现了一项功能,允许用户在厌倦等待时单击“取消”按
如何找到所有可能直接或间接调用给定方法的单元测试?当我更改方法时,我希望知道要运行的最佳测试;必须有一个工具! 因为我们有很多接口(interface),所以我对所有调用接口(interface)方法
我想知道,一个类会被装箱吗?我一直假设每个类都有一个虚拟表,可以用来标识类,所以它需要装箱吗? 最佳答案 只有值类型(结构)被装箱。类实例不会被装箱。 关于.net - 类是盒装的吗? 。网,我们在S
所以接下来有一个按钮调用(页面)。它的 href 链接是 site/blah/#。所以我知道它真正运行的 javascript 代码。在我解析完第一页后,我想解析下一页。我如何模拟鼠标点击,以便我可以
我想知道是否有人对解决以下设计问题有好的建议/模式。我有一个命令类的层次结构。在最抽象的层面上,我有一个 ICommand 接口(interface)。执行 ICommand 的 RunCommand
我在资源(xsd 文件)中有几个文件可用于验证收到的 xml 消息。我使用的资源文件名为 AppResources.resx,它包含一个名为 clientModels.xsd 的文件。当我尝试使用这样
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
(最后的简短版本) 我目前正在开发的软件需要跟踪任意数量的 MS Office 文件,现在需要提供将所有这些文件一起打印的功能,以及一些应用程序数据(可能会发送到打印机作为 .xps、.html 或
我想在不指定命名空间或程序集的情况下按名称(字符串)实例化一个类。像这样(Unity 语法): var processor = container.Resolve("SpecialProcessor"
我有一些代码可以对 64 位整数进行大量比较,但是它必须考虑数字的长度,就好像它被格式化为字符串一样。我无法更改调用代码,只能更改函数。 最简单的方法(除了 .ToString().Length 之外
使用遗留代码,我发现我有很多这样的语句(超过 500 个) bool isAEqualsB = (a == b) ? true : false; 这样重写有意义吗? bool isAEqualsB =
我有这个: AudioPlayer player = new AudioPlayer(); player.Directory = vc.Directory; player.StartTime = vc
我已经阅读了很多关于双重检查锁定的危险的文章,我会努力远离它,但话虽如此,我认为他们的阅读非常有趣。 我正在阅读 Joe Duffy 的这篇关于使用双重检查锁定实现单例的文章: http://www.
对于可变类型,值类型和引用类型之间的行为差异很明显: // Mutable value type PointMutStruct pms1 = new PointMutStruct(1, 2); P
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我有一个 Cordova pp 我在 Controller 中调用post方法 它可以在浏览器中工作,但是在构建和调试apk时出现错误 ionic.bundle.js:23826 POST http:
我们正在尝试将时间戳附加到某些 URL 以让内容缓存但在它们发生更改时更新它们。我们有代码可以归结为: DateTime ts = File.GetLastWriteTime(absPath); 其中
我是一名优秀的程序员,十分优秀!