c# - WPF - 如何将元素设置为逐行标记,而不是行索引
问题描述
我正在寻找一种方法来执行该SetRow(UIElement element, int value)
方法,但不是使用 int 值来放置元素,而是使用行的标记来查找特定行。
基本上是这样的:
RowDefinition newRow = new RowDefinition();
newRow.Tag = "This is the row I want to find";
TextBlock newBlock = new TextBlock();
Grid.SetRow(newBlock, "This is the row I want to find");
然后它会根据标签找到该行并将文本块放在那里。是否有一种预先存在的方法可以做到这一点,或者是否有人对如何为其制作自定义方法有任何想法?
解决方案
如果newRow
和newBlock
已经被添加到同一个网格中,你可以这样做
var rowTag = "This is the row I want to find";
if (newBlock.Parent is Grid grid)
{
var row = grid.RowDefinitions.FirstOrDefault(r => rowTag.Equals(r.Tag));
if (row != null)
{
Grid.SetRow(newBlock, grid.RowDefinitions.IndexOf(row));
}
}
推荐阅读
- c - 为什么“float”和“double”数据类型在 C 编程中的输出中添加 0?
- angular - Ionic4/Angular 8 - HTML 视频标签不播放 base64 字符串
- vue.js - 如何使用此 Vuetify 示例在 Y 轴上获取值
- python - 如何在不复制熊猫数据框中的其他列值的情况下分解列值?
- r - 如何从 R 中的给定答案创建一组虚拟变量?
- excel - 是否可以通过 OR 语句使用具有多个条件的 INDEX 和 MATCH?
- r - 动态分配/引用 data.table 中的列名(在 i、j 和 by 中)
- android - Places SDK for Android:设置自动完成活动的样式
- php - php 上有效 json 上的 json_last_error_msg() 上的 json_decode() 语法错误
- regex - 如何使用 RegExp 过滤部分