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

问题描述 投票:2回答:1

我在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???
        }
    }
}
c# wpf
1个回答
1
投票

你可以这样阅读DataGrid.Items的商品:

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

但是,最好采用强类型方法。在这种情况下,您需要将ItemsSourceDataGrid绑定到ObservableCollection<DataType>,其中DataType表示每行的数据结构。 (例如,它具有可绑定的URL属性,依此类推)

这是一个例子:

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

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

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

RowVm在哪里:

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并从Items读取

© www.soinside.com 2019 - 2024. All rights reserved.