正在从WPF ListView读取数据。C#
如何从WPF列表视图读取数据 这是我的密码正在从WPF ListView读取数据。C#,c#,wpf,listview,C#,Wpf,Listview,如何从WPF列表视图读取数据 这是我的密码 <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&g
<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>
完成添加文件后,我的ListView将如下所示
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; }
}
|状态|文件名|
|[图片]| test.txt|
|[图片]| test1.txt|
(对不起,我没有足够的声誉发布图片)
现在我如何从第二列读取“文件名”
我对WPF很陌生。
提前感谢。简而言之,您应该将项目集合(每行一个)数据绑定到
列表视图。项目资源
属性:
<ListView ItemsSource="{Binding SomeCollection}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
<ListView ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding CurrentItem}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
这种情况的一个改进是将另一个与数据绑定集合上的对象类型相同的属性绑定到列表视图。SelectedItem
属性:
<ListView ItemsSource="{Binding SomeCollection}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
<ListView ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding CurrentItem}">
<ListView.View>
<!-- Define your view here -->
</ListView.View>
</ListView>
请参阅MSDN上的页面以获取更多帮助。不确定“从ListView读取”的确切含义,但您通常会访问StackItems类(也称为视图模型),并从其TBtxt属性(应该有更好的名称)获取文件名。与将StackItems实例添加到ListView的Items集合然后忘记它们不同,您可以将它们添加到
ObservableCollection
,并将ListView的ItemsSource属性绑定到该集合。在web上搜索MVVM。还要注意,您通常不会在WPF应用程序中使用类型Image
,因为它来自WinForms。改用ImageSource
。