从XAML文件填充与复选框列表视图 [英] Populating a listview with checkboxes from a XAML file

查看:109
本文介绍了从XAML文件填充与复选框列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,非常感谢戴夫·克莱默和Jasti为我安排这么远!

First, many thanks to Dave Clemmer and Jasti for getting me this far!

我有一个ListView看起来像这样(除了Load按钮已经被添加,因为这截屏拍摄):

I've got a listview that looks like this (except a Load button has been added since this screen shot was taken):

这里的XAML code:

Here's the XAML code:

<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 ItemsSource="{Binding Path=lsvData, Mode=TwoWay}" Name="listView1">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="152" Header="Name" DisplayMemberBinding="{Binding Path=Name}"  />
                    <GridViewColumn Width="202" Header="Picture of Young Plant" DisplayMemberBinding="{Binding Path=YoungPic}" CellTemplate="{StaticResource YoungPicCell}" />
                    <GridViewColumn Width="202" Header="Picture of Medium Plant"  DisplayMemberBinding="{Binding Path=MediumPic}" CellTemplate="{StaticResource MediumPicCell}" />
                    <GridViewColumn Width="202" Header="Picture of Adult Plant"  DisplayMemberBinding="{Binding Path=AdultPic}" 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" DockPanel.Dock="Bottom" Height="23" HorizontalAlignment="Left" Margin="26,240,0,0" Name="LoadPlants" VerticalAlignment="Top"  Click="LoadPlants_Click" Width="75" /> 
    </Grid>
</DockPanel>

下面是当你点击Load按钮的加载XML文件:

Here is the XML file that is being loaded when you click on the Load button:

<?xml version="1.0" encoding="UTF-8"?>
<Plants>
<Species>
    <Name>Alethopteris</Name>
    <YoungPic>AlethopterisYoung.bmp</YoungPic>
    <MediumPic>AlethopterisMedium.bmp</MediumPic>
    <AdultPic>AlethopterisAdult.bmp</AdultPic>
    <Saltwater>true</Saltwater>
    <FreshWater>False</FreshWater>
    <Grasslands>False</Grasslands>
    <Swamp>False</Swamp>
    <TropicalForrest>False</TropicalForrest>
    <Forest>False</Forest>
    <ForestEdge>False</ForestEdge>
    <Sand>False</Sand>
    <Coastal>False</Coastal>
    <RiverBorder>False</RiverBorder>
    <LakeBorder>False</LakeBorder>
    <Floodplain>False</Floodplain>
</Species>
</Plants>

和,最后,这里的相关code背后:

And, lastly, here's the relevant code behind:

      public PlantDisplay()
    {
        InitializeComponent();
        _wvm = new WindowViewModel();
        this.DataContext = _wvm;
    }

    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; }
    }


    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> _plantList = new ObservableCollection<LVData>();
        public ObservableCollection<LVData> lsvData
        {
            get { return _plantList; }
            set { _plantList = value; RaisePropertyChanged("lsvData"); }
        }

        public void PopulateDataFromXML()
        {
            XDocument loaded = XDocument.Load(@"DinoIslandPlants.xml");

            var Plants = from x in loaded.Descendants("Plants")
                          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,
                              SaltWater = x.Descendants("SaltWater").First().Value 
                          };
            foreach (var _plant in Plants)
            {
                _plantList.Add(new LVData { 
                    Name = _plant.Name, 
                    YoungPic = _plant.YoungPic, 
                    MediumPic = _plant.MediumPic, 
                    AdultPic = _plant.AdultPic, 
                    SaltWater = Convert.ToBoolean(_plant.SaltWater) });
            }

            RaisePropertyChanged("lsvData");
        }

    }

我有两个问题:


  1. 它的崩溃在这条线:盐水= Convert.ToBoolean(_plant.SaltWater)});

  2. 这不是装工厂的形象,而是显示路径字符串。

任何意见,建议?
为我节省了XAML文件中boolean值的方法是否正确?

Any ideas, suggestions? Is the way I'm saving a boolean in the XAML file correct?

在此先感谢!

推荐答案

有关问题1的问题是,在你的XML你有项目咸水。
在您的code你有这一行。

The problem regarding question 1 is that in your XML you have the item Saltwater. In your code you have this line.

SaltWater = x.Descendants("SaltWater").First().Value 

注意咸水和海水之间的区别?如果你解决你的情况,我相信你会选择那一问题。

Notice the difference between SaltWater and Saltwater? If you fix your case I believe you will sort that problem.

我不知道为什么。首先()。值不会抛出异常。也许其他人可以回答你。

I am not sure why .First().Value does not throw an exception. Maybe another person could answer that for you.

这篇关于从XAML文件填充与复选框列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆