【问题标题】:Iterate through a specific row in WPF Grid遍历 WPF Grid 中的特定行
【发布时间】:2019-01-03 07:54:53
【问题描述】:

我想在WpfGrid 中遍历特定的Row,并在此行的每个单元格中获取UI Element。我已经搜索了很多,但没有找到任何解决方案! UI 元素的类型为TextBlock

说明
正如您在附图中看到的那样。我有一个Grid,其中有一些RowsColumns。我想遍历Row 0,并将Row 0 的每个cell 内部的TextBlock.Text 与一些text 进行比较。怎么做 ?

【问题讨论】:

  • 在迭代时你想对 UI 元素做什么?
  • @DanielW。因为 Ui 元素是TextBlock。我想得到它的TextBlock.Text 属性!
  • 这听起来并不难。您在将对象转换为 UIElement 时遇到问题吗?
  • @kennyzx 老实说,我尝试了很多,但现在对我来说有点难。投射不是问题.. idk 如何遍历特定行及其每个单元格!
  • 如果您只想拥有文本,为什么不使用绑定并遍历绑定对象/列表中的值?

标签: c# wpf grid


【解决方案1】:

首先你可以获得 Grid 的 Children 集合,然后你可以检查每个元素它所在的行:

foreach (var element in grid1.Children) {
    if (grid1.GetRow(element) == 0) {
        //now you get all the UIElements in the first row
    }
}

【讨论】:

    【解决方案2】:

    为了保持 View 和 ViewModel devided 而不是使用 CodeBehind 进行逻辑,我建议使用 DataGrid 和 Binding 进行搜索,所以 View 就像:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
    
        <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="0"></TextBox>
        <TextBlock Text="{Binding SearchResult}" Grid.Row="0" Grid.Column="1"></TextBlock>
    
        <DataGrid ItemsSource="{Binding Entries}" Grid.Row="1" Grid.ColumnSpan="2">
        </DataGrid>
    </Grid>
    

    所以 SearchText 是要搜索的内容,而 SearchResult 是结果。两者都绑定到 ViewModel。并且 Connected View Model 包含 DoSearchBay() 用于使用当前 SearchText 的值更新 SearchResult 字段,代码如下:

    public class ViewModel: INotifyPropertyChanged
    {
        private String _searchText = "Bay?";
        private String _searchResult = "N/A";
    
        public ObservableCollection<MyDataObject> Entries { get; set; }
    
        public string SearchText
        {
            get => _searchText;
            set
            {
                if (value == _searchText) return;
                _searchText = value;
                DoSearchBay();
                OnPropertyChanged();
            }
        }
    
        private void DoSearchBay()
        {
            var sel = Entries.Select((dm, index) => new { index, dm.Bay}).FirstOrDefault(obj => obj.Bay.Equals(_searchText, StringComparison.OrdinalIgnoreCase)) ;
            if (sel != null)
            {
                SearchResult = "Found in row " + sel.index;
            }
            else
            {
                SearchResult = "N/A";
            }
        }
    
        public string SearchResult
        {
            get => _searchResult;
            set
            {
                if (value == _searchResult) return;
                _searchResult = value;
                OnPropertyChanged();
            }
        }
    
    
        public ViewModel()
        {
            //Create Fake values
            Entries = new ObservableCollection<MyDataObject>();
            Entries.Add(new MyDataObject() {Bay = "Bay1", Am9 = "value1-1", Am10 = "value1-2", Am11 = "value1-3" });
            Entries.Add(new MyDataObject() { Bay = "Bay2", Am9 = "value2-1", Am10 = "value2-2", Am11 = "value2-3" });
            Entries.Add(new MyDataObject() { Bay = "Bay3", Am9 = "value3-1", Am10 = "value1-2", Am11 = "value3-3" });
        }
    
        // ToDo Implement INotifyPropertyChanged...
    }
    

    包含数据的模型看起来像:

    public class MyDataObject
    {
        public String Bay { get; set; }
        public String Am9 { get; set; }
        public String Am10 { get; set; }
        public String Am11 { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      相关资源
      最近更新 更多