是否有简单/标准的方法从文件读取到列表视图并从列表视图保存到文件?我真的不在乎它是TXT文件,XAML文件还是二进制文件。这并不重要,我没有那么多数据。
以下是存储在每个
中的列和数据 public class LVData
{
public string Name { get; set; }
public string YoungPic { get; set; }
public string MediumPic { get; set; }
public string AdultPic { get; set; }
public bool SaltWater { get; set; }
public bool FreshWater { get; set; }
public bool Grasslands { get; set; }
public bool Swamp { get; set; }
public bool TropicalForrest { get; set; }
public bool Forest { get; set; }
public bool ForestEdge { get; set; }
public bool Sand { get; set; }
public bool Coastal { get; set; }
public bool RiverBorder { get; set; }
public bool LakeBorder { get; set; }
public bool Floodplain { get; set; }
}
我的列表视图名为listView1。
和XAML:
<Window x:Class="DinosaurIsland.PlantDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DinosaurIsland"
Title="Vegetation" Height="308" Width="1212" WindowStyle="SingleBorderWindow"
DataContext="{Binding RelativeSource={RelativeSource Self}}" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="YoungPicCell">
<StackPanel Orientation="Horizontal">
<Image Height="200" Width="200" Stretch="None" Source="{Binding YoungPic}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="MediumPicCell">
<StackPanel Orientation="Horizontal">
<Image Height="200" Width="200" Stretch="None" Source="{Binding MediumPic}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="AdultPicCell">
<StackPanel Orientation="Horizontal">
<Image Height="200" Width="200" Stretch="None" Source="{Binding AdultPic}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="TerrainCell">
<StackPanel Orientation="Vertical">
<CheckBox Content="Salt Water" Name="SaltWaterCheckbox" IsThreeState="False" IsChecked="{Binding Saltwater}" />
<CheckBox Content="Fresh Water" Name="FreshWaterCheckbox" IsThreeState="False" IsChecked="{Binding Freshwater}" />
<CheckBox Content="Grassland / Plains" Name="GrasslandsCheckbox" IsThreeState="False" IsChecked="{Binding Grassland}" />
<CheckBox Content="Swamp" Name="SwampCheckbox" IsThreeState="False" IsChecked="{Binding Swamp}" />
<CheckBox Content="Tropical Forest" Name="TropicalForestCheckbox" IsThreeState="False" IsChecked="{Binding TropicalForest}" />
<CheckBox Content="Forest" Name="ForestCheckbox" IsThreeState="False" IsChecked="{Binding Forest}" />
<CheckBox Content="Forest Edge" Name="ForestEdgeCheckbox" IsThreeState="False" IsChecked="{Binding ForestEdge}" />
<CheckBox Content="Sand" Name="SandCheckbox" IsThreeState="False" IsChecked="{Binding Sand}" />
<CheckBox Content="Coastal" Name="CoastalCheckbox" IsThreeState="False" IsChecked="{Binding Coastal}" />
<CheckBox Content="River Border" Name="RiverBorderCheckbox" IsThreeState="False" IsChecked="{Binding RiverBorder}" />
<CheckBox Content="LakeBorder" Name="LakeBorderCheckbox" IsThreeState="False" IsChecked="{Binding LakeBorder}" />
<CheckBox Content="Floodplain" Name="FloodplainCheckbox" IsThreeState="False" IsChecked="{Binding Floodplain}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="PlacePlantsCell">
<StackPanel Orientation="Vertical">
<Label Margin="10" Content="Random" HorizontalAlignment="Center" ></Label>
<Slider Margin="10" Width="190" Value="50" Orientation="Horizontal" HorizontalAlignment="Center" IsSnapToTickEnabled="True" Maximum="100" TickPlacement="BottomRight" TickFrequency="5"> </Slider>
<Button Margin="10" Content="Randomly Seed Plants" HorizontalAlignment="Center" Height="23" Name="SeedButton" ></Button>
</StackPanel>
</DataTemplate>
</Window.Resources>
<DockPanel>
<Grid>
<ListView Name="listView1">
<ListView.View>
<GridView>
<GridViewColumn Width="152" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="202" Header="Picture of Young Plant" CellTemplate="{StaticResource YoungPicCell}" />
<GridViewColumn Width="202" Header="Picture of Medium Plant" CellTemplate="{StaticResource MediumPicCell}" />
<GridViewColumn Width="202" Header="Picture of Adult Plant" CellTemplate="{StaticResource AdultPicCell}" />
<GridViewColumn Width="202" Header="Terrain / Environments" CellTemplate="{StaticResource TerrainCell}" />
<GridViewColumn Width="202" Header="Place Plants" CellTemplate="{StaticResource PlacePlantsCell}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="New Plant" DockPanel.Dock="Bottom" Height="23" HorizontalAlignment="Left" Margin="160,240,0,0" Name="NewPlant" VerticalAlignment="Top" Width="75" Click="NewPlant_Click" />
<Button Content="Save" DockPanel.Dock="Bottom" Height="23" HorizontalAlignment="Center" Margin="1099,240,15,0" Name="SavePlant" VerticalAlignment="Top" Width="75" Click="SavePlant_Click"/>
<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="26,240,0,0" Name="LoadPlants" VerticalAlignment="Top" Click="LoadPlants_Click" Width="75" />
</Grid>
</DockPanel>
这是整个表单/对话框/列表视图(无论它叫什么)的图片: Listview image http://dinosaur-island.com/wordpress/wp-content/uploads/2013/06/Seeding-Plants.jpg
我已经添加了一个“加载”按钮,因为这个屏幕截图已被拍摄。
答案 0 :(得分:1)
我编写了一个小项目,用于从XML读取数据并将其显示在ListView
。
以下是示例XML文件
<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person>
<Name>Dave</Name>
<YoungPic>Picture1</YoungPic>
<MediumPic>Picture2</MediumPic>
<AdultPic>Picture3</AdultPic>
<Terrain>1</Terrain>
</Person>
<Person>
<Name>Rachael</Name>
<YoungPic>Picture1</YoungPic>
<MediumPic>Picture2</MediumPic>
<AdultPic>Picture3</AdultPic>
<Terrain>2</Terrain>
</Person>
</Persons>
具有双向绑定ListView的XAML代码
<Window x:Class="lvs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="400">
<Grid>
<ListView ItemsSource="{Binding Path=lsvData, Mode=TwoWay}"
Name="listview1" HorizontalAlignment="Left" Margin="0,0,0,63" Width="262">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding Path=YoungPic}" Header="Young Pic" />
<GridViewColumn DisplayMemberBinding="{Binding Path=MediumPic}" Header="Medium Pic" />
<GridViewColumn DisplayMemberBinding="{Binding Path=AdultPic}" Header="Adult Pic" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Terrain}" Header="Terrain" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="62,0,0,12" Name="Load" VerticalAlignment="Bottom" Width="146" Click="button1_Click" />
<Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="224,0,0,12" Name="Save" VerticalAlignment="Bottom" Width="128" />
</Grid>
</Window>
以下是Mainwindow.xaml.cs的代码
public partial class MainWindow : Window
{
private WindowViewModel _wvm;
public MainWindow()
{
InitializeComponent();
_wvm = new WindowViewModel();
this.DataContext = _wvm;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
_wvm.PopulateData();
}
}
然后在同一个项目命名空间中添加以下两个类 - Viewmodel和lsvData
public class LVData
{
public string Name { get; set; }
public string YoungPic { get; set; }
public string MediumPic { get; set; }
public string AdultPic { get; set; }
public byte Terrain { get; set; }
}
public class WindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
//called when a property is changed
protected void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
private ObservableCollection<LVData> _personList = new ObservableCollection<LVData>();
public ObservableCollection<LVData> lsvData
{
get { return _personList; }
set { _personList = value; RaisePropertyChanged("lsvData"); }
}
public void PopulateDataFromXML()
{
XDocument loaded = XDocument.Load(@"c:\documents and settings\bjasti\my documents\visual studio 2010\Projects\lvs\lvs\data.xml");
var Persons = from x in loaded.Descendants("Person")
select new
{
Name = x.Descendants("Name").First().Value,
YoungPic = x.Descendants("YoungPic").First().Value,
MediumPic = x.Descendants("MediumPic").First().Value,
AdultPic = x.Descendants("AdultPic").First().Value,
Terrain = x.Descendants("Terrain").First().Value
};
foreach (var _person in Persons)
{
_personList.Add(new LVData { Name = _person.Name, YoungPic = _person.YoungPic, MediumPic = _person.MediumPic, AdultPic = _person.AdultPic, Terrain = Convert.ToByte(_person.Terrain) });
}
RaisePropertyChanged("lsvData");
}
}
检查此解决方案并让我知道它是否有效