如何从WPF中的Gridview.Items中读取特定列?

时间:2018-08-30 08:54:30

标签: c# wpf

我在GridView列URL中插入了多个URL。现在,我想读取 URL 列中的所有URL。我不确定如何从列URL一次打开URL。我尝试加载 {Binding URL} ,但这是不可能的。

是否可以从GridView列URL中读取文本?还是我必须尝试其他方法?

以下是XAML:

<Grid RenderTransformOrigin="0.5,0.5">

    <Grid.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
        </TransformGroup>

    </Grid.RenderTransform>

    <Label x:Name="label1" Content="URL :" HorizontalAlignment="Left" Margin="22,27,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="146" Margin="64,30,0,0" TextWrapping="Wrap" Text="Enter URLs Here" VerticalAlignment="Top" Width="555" AcceptsReturn="True"/>
    <Button x:Name="button1" Content="Load" HorizontalAlignment="Left" Margin="652,19,0,0" VerticalAlignment="Top" Width="134" Height="40" Click="Button1_Click"/>
    <Grid Margin="0,0,0,0.333">

        <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="225" VerticalAlignment="Top" Width="761" Margin="22,185,0,0" AutoGenerateColumns="True">
            <DataGrid.Columns>

                <DataGridHyperlinkColumn Header="URL" Width="150" Binding="{Binding URL}"/>
                <DataGridHyperlinkColumn Header="Source" Width="150" Binding="{Binding Source}"/>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>      

</Grid>

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace AIT
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            List<string> Urllines = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (string strUrl in Urllines)
                dataGrid1.Items.Add(new { URL = strUrl});
        }

        private void ReadUrls()
        {
            foreach (string item in dataGrid1.Items)
                //item.URL???
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以像这样从DataGrid.Items阅读项目:

foreach (var item in dataGrid1.Items)
{
    string url = (string)((dynamic)item).URL;
    //...
}

但是,最好使用强类型方法。在这种情况下,您需要将ItemsSource的{​​{1}}绑定到DataGrid,其中ObservableCollection<DataType>代表每一行的数据结构。 (例如,它具有可绑定的URL属性等等)

这是一个例子:

DataType

其中root是窗口的x:Name。并将以下代码添加到后面的Windows代码中:

<DataGrid ItemsSource="{Binding ElementName=root, Path=Items}" ...>

其中RowVm是:

    private ObservableCollection<RowVm> _items = new ObservableCollection<RowVm>();
    public ObservableCollection<RowVm> Items { get { return _items; } }

因此,除了添加到public class RowVm : DependencyObject { public string URL { get { return (string)GetValue(URLProperty); } set { SetValue(URLProperty, value); } } public static readonly DependencyProperty URLProperty = DependencyProperty.Register("URL", typeof(string), typeof(RowVm), new PropertyMetadata("")); public string Source { get { return (string)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(string), typeof(RowVm), new PropertyMetadata("")); } 之外,您还将添加到dataGrid1.Items并从Items中读取