/ Čítanie údajov z WPF ListView. C # - c #, wpf, listview

Čítanie údajov z WPF ListView. C # - c #, wpf, zobrazenie zoznamu

Ako čítať údaje z WPF ListView?

Tu je môj kód.

        <ListView x:Name="LVR"  AllowDrop="True" PreviewDrop="LVR_PreviewDrop" RenderTransformOrigin="0.505,0.506" Margin="0,0,0,0" Grid.Row="1" Grid.ColumnSpan="3" MouseEnter="LVR_MouseEnter" >
<ListView.View>
<GridView >
<GridViewColumn Header="Status" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="index.png" Width="26"></Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="File Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TBtxt}" FontWeight="Bold"  Foreground="Blue" Cursor="Hand" Height="30" TextAlignment="Left" HorizontalAlignment="Center"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

A takto vkladám položky do zoznamu.

void Insert()
{
WinForms.OpenFileDialog ofd = new WinForms.OpenFileDialog();
ofd.Multiselect = true;
ofd.Title = "Select .TXT File";
ofd.FileName = "";
ofd.Filter = "TXT | *.txt";
if (ofd.ShowDialog() == WinForms.DialogResult.OK)
{
foreach (var filename in ofd.FileNames)
{
if (System.IO.Path.GetExtension(filename).ToUpperInvariant() == ".txt")
{
LVR.Items.Add(new StackItems { TBtxt = filename });
}
}
}
}
class StackItems
{
public string TBtxt { get; set; }
public Image imgg { get; set; }
}

Po dokončení pridávania súborov bude môj ListView vyzerať takto.

| Stav | Názov súboru |

| [Obrázok] | test.txt |

| [Obrázok] | test1.txt |

(Prepáč. Nemám dostatočnú reputáciu na zverejnenie obrázka).

Ako teraz prečítam „Názov súboru“ z druhého stĺpca?

Som pre WPF veľmi nový. Vopred ďakujem.

odpovede:

0 pre odpoveď č. 1

Stručne povedané, mali by ste byť dátový zväzok zbierky položiek (jeden pre každý riadok) k ListView.ItemsSource ubytovanie:

<ListView ItemsSource="{Binding SomeCollection}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>

Ak to urobíte, prístup k položkám je taký jednoduchý ako tento (pomocou Linq):

var firstItem = SomeCollection.First();

Vylepšením tejto situácie by bolo viazanie údajov na inú vlastnosť rovnakého typu ako objekty v zbierke viazaných na údaje ListView.SelectedItem ubytovanie:

<ListView ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding CurrentItem}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>

Týmto spôsobom získate prístup k vlastnostiam z aktuálne vybratej položky z priečinka ListView ako toto:

int someValue = CurrentItem.SomeProperty;

Pozrite si časť ListView Trieda stránku na MSDN pre ďalšiu pomoc.