如何从 ListView 中的 GridView 中的选定行的所有列中检索文本?

How to retrieve text from all columns of selected row from a GridView within a ListView?

我正在尝试从连接到 ListView 的 GridView 的所有列(所选项目的)中检索文本。虽然数据显示正确,但当我使用 ListView 本身的 ToString() 检索所选行的内容时,它 returns 命名空间和 class 名称而不是 [=26] 中的文本=].到目前为止,这是我尝试过的方法:

覆盖 ToString() 方法(对我不起作用,因为我的代码有多个属性),

在 ListView

中使用 ItemsSource

我的代码(XAML):

<ListView ItemsSource="{Binding}" x:Name="ListViewR" Padding="0" VerticalContentAlignment="Stretch" Width="321"
                     BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="0"
                     MouseDoubleClick="ListViewR_MouseDoubleClick" >
                <ListView.View>
                    <GridView x:Name="GridView">
                        <GridViewColumn Header="Resolution" DisplayMemberBinding="{Binding Resolution}" ></GridViewColumn>
                        <GridViewColumn Header="Refresh rate" DisplayMemberBinding="{Binding RefreshRate}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

代码隐藏 (C#):

// the properties class
public class ResItems
{
     public string Resolution { get; set; }
     public string RefreshRate { get; set; }
}

// method that populates ListBox & GridView
private void ListDisplaySettings() {
            myProgram.DEVMODE vDevMode = new syslib32.DEVMODE();
            int i = 0;
            while (Res.EnumDisplaySettingsA(null!, i, ref vDevMode)) {
                this.ListViewR.Items.Add(new ResItems{Resolution = vDevMode.dmPelsWidth + "x" + vDevMode.dmPelsHeight, RefreshRate = vDevMode.dmDisplayFrequency + " Hz",});
                i++;
            }
        }

// failing method
private void GetResolution() {
            if (this.ListViewR.SelectedItems.Count <= 0) return;
            string s = this.ListViewR.SelectedItem.ToString(); // here's where the unexpected things occur. As you can see, I am trying to parse the contents of the selected row of the GridView/ListView to change the user's screen resolution (with safety of course).
            System.Windows.MessageBox.Show(s);
            string[] v = s.Split(new[] {"x", "@", "Hz", "bpp",}, System.StringSplitOptions.RemoveEmptyEntries);
            uint q = uint.Parse(v[0]);
            uint b = uint.Parse(v[1]);
            uint z = uint.Parse(v[2]);
            uint f = uint.Parse(v[3]);
            myProgram.CResolution.ChangeRes(q, b, z, f);
            this.HzControl.Value = Res.CRefreshRate;
        }

SelectedItem 属性 具有 object 类型,但由于您知道这些对象的具体类型,因此直接进行类型转换 abd 访问 属性:

private void GetResolution() 
{
    if (this.ListViewR.SelectedItems.Count <= 0) return;

    var SelectedResItems = (ResItems)this.ListViewR.SelectedItem;
    string s = SelectedResItems.Resolution;

    System.Windows.MessageBox.Show(s);
    string[] v = s.Split(new[] {"x", "@", "Hz", "bpp",}, System.StringSplitOptions.RemoveEmptyEntries);
    uint q = uint.Parse(v[0]);
    uint b = uint.Parse(v[1]);
    uint z = uint.Parse(v[2]);
    uint f = uint.Parse(v[3]);
    myProgram.CResolution.ChangeRes(q, b, z, f);
    this.HzControl.Value = Res.CRefreshRate;
}