将ListView的ItemsSource绑定到已绑定属性的属性

时间:2015-09-03 16:09:36

标签: c# wpf listview

我在列表视图中遇到了列表视图问题,在列表视图中有一个组合框。

我有一个listview(名为' lvUsers'),某些列包含组合框,每个列包含一个listview(名为' lvArmamento')。

当我以编程方式设置主列表视图的ItemsSource时,lvUsers,它没有任何问题。

但是当我尝试在XAML中使用listview' lvArmamento'时,它不会显示任何内容。

这是我的XAML代码:

<Window x:Class="WpfTutorialSamples.ListView_control.ListViewColumnSortingSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListViewColumnSortingSample" Height="200" Width="550">
    <Grid Margin="10">
        <ListView Name="lvUsers">
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="120" DisplayMemberBinding="{Binding Transponder}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Transponder">Transponder</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="80" DisplayMemberBinding="{Binding Tipo}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Tipo">Tipo</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="110" DisplayMemberBinding="{Binding Destruida}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Destruida" >Destruida</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>

                    <GridViewColumn Width="80" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Combustible">Combustible</GridViewColumnHeader>
                        </GridViewColumn.Header>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>

                                <ComboBox  SelectionChanged="ComboBox_StopChanges" DataContext="{Binding combustible}">
                                    <ComboBox.Resources>
                                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">White</SolidColorBrush>
                                    </ComboBox.Resources>

                                    <ComboBox.Items>
                                        <StackPanel Orientation="Horizontal">
                                            <StackPanel>
                                                <StackPanel Name="stckContentLeft">
                                                    <Label BorderBrush="White" BorderThickness="1" Content="Loaded :" FontSize="12" FontWeight="SemiBold" Foreground="Black" Height="25" HorizontalAlignment="Left" Margin="2,2,0,0" VerticalAlignment="Top"  Width="95"/>
                                                    <Label BorderBrush="White" BorderThickness="1" Content="Used :" FontSize="12" FontWeight="SemiBold" Foreground="Black" Height="25" HorizontalAlignment="Left" Margin="2,2,0,0" VerticalAlignment="Top"  Width="95"/>
                                                </StackPanel>
                                            </StackPanel>
                                            <StackPanel>
                                                <StackPanel Name="stckContentRigth">
                                                    <TextBox Name="textBoxLoaded" Height="25" Width="115" MaxLength="3" Margin="2,2,0,0" BorderBrush="Black" BorderThickness="1" FontSize="12" FontWeight="SemiBold" HorizontalAlignment="Left" Text="{Binding Cargado}" VerticalAlignment="Top" />
                                                    <TextBox Name="textBoxUsed" Height="25" Width="115" MaxLength="3" Margin="2,2,0,0" BorderBrush="Black" BorderThickness="1" FontSize="12" FontWeight="SemiBold" HorizontalAlignment="Left" Text="{Binding Usado}" VerticalAlignment="Top" />
                                                </StackPanel>
                                            </StackPanel>
                                        </StackPanel>
                                    </ComboBox.Items>
                                </ComboBox>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Width="80" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Armamento">Armamento</GridViewColumnHeader>
                        </GridViewColumn.Header>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>

                                <ComboBox  SelectionChanged="ComboBox_StopChanges" >
                                    <ComboBox.Resources>
                                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">White</SolidColorBrush>
                                    </ComboBox.Resources>

                                    <ComboBox.Items>
                                        <ListView Margin="10" Name="lvArmamento" ItemsSource="{Binding armamento}">
                                            <ListView.View>
                                                <GridView>

                                                    <GridViewColumn Header="Tipo" Width="80" DisplayMemberBinding="{Binding Tipo}" />
                                                    <GridViewColumn Header="Cantidad" Width="80" DisplayMemberBinding="{Binding Cantidad}" />
                                                    <GridViewColumn Header="Acierto" Width="80" DisplayMemberBinding="{Binding Acierto}" />
                                                    <GridViewColumn Header="Velocidad" Width="80" DisplayMemberBinding="{Binding Velocidad}" />
                                                    <GridViewColumn Header="Daño" Width="80" DisplayMemberBinding="{Binding Dano}" />
                                                    <GridViewColumn Header="Alcance" Width="80" DisplayMemberBinding="{Binding Alcance}" />
                                                    <GridViewColumn Header="Peso" Width="80" DisplayMemberBinding="{Binding Peso}" />
                                                    <GridViewColumn Header="Precio" Width="80" DisplayMemberBinding="{Binding Precio}" />
                                                    <GridViewColumn Header="Usado" Width="150" DisplayMemberBinding="{Binding Usado}" />
                                                </GridView>
                                            </ListView.View>
                                        </ListView>
                                    </ComboBox.Items>
                                </ComboBox>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>


                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

这是我的C#代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;

using System.Diagnostics;

namespace WpfTutorialSamples.ListView_control
{
    public partial class ListViewColumnSortingSample : Window
    {

        public ListViewColumnSortingSample()
        {
            InitializeComponent();

            List<Armamento> armamento_m001 = new List<Armamento>();
            armamento_m001.Add(new Armamento() { Tipo = "M001", Cantidad = "M001", Acierto = "M001", Velocidad = "M001", Dano = "M001", Alcance = "M001", Peso = "M001", Precio = "M001", Usado = "M001" });
            armamento_m001.Add(new Armamento() { Tipo = "fdfd", Cantidad = "M00fd1", Acierto = "M00f1", Velocidad = "Md001", Dano = "M0s01", Alcance = "Ma001", Peso = "M00a1", Precio = "M0s01", Usado = "M0d01" });

            List<User> items = new List<User>();
            items.Add(new User() { Transponder = "M001", Tipo = "UH60", Destruida = Estado_Aeronave.Destruida, combustible = new Combustible() { Cargado = "4000", Usado = "3000" }, armamento = armamento_m001 });
            items.Add(new User() { Transponder = "M002", Tipo = "UH70", Destruida = Estado_Aeronave.Funcional, combustible = new Combustible() { Cargado = "3000", Usado = "2000" } });
            items.Add(new User() { Transponder = "M003", Tipo = "UH80", Destruida = Estado_Aeronave.Funcional, combustible = new Combustible() { Cargado = "2000", Usado = "1000" } });
            items.Add(new User() { Transponder = "M004", Tipo = "UH90", Destruida = Estado_Aeronave.Destruida, combustible = new Combustible() { Cargado = "1000", Usado = "500" } });

            //programatically set itemsource of the main listview
            lvUsers.ItemsSource = items;
        }




        //Prevents the combobox of the main list from showing its content
        private void ComboBox_StopChanges(object sender, SelectionChangedEventArgs e)
        {
            ComboBox snd = sender as ComboBox;
            snd.SelectedIndex = -1;
        }
    }

    public enum Estado_Aeronave { Destruida, Funcional };

    public class Combustible
    {
        public string Cargado { get; set; }
        public string Usado { get; set; }
    }

    public class Armamento
    {
        public string Tipo { get; set; }
        public string Cantidad { get; set; }
        public string Acierto { get; set; }
        public string Velocidad { get; set; }
        public string Dano { get; set; }
        public string Alcance { get; set; }
        public string Peso { get; set; }
        public string Precio { get; set; }
        public string Usado { get; set; }
    }

    public class User
    {
        public string Transponder { get; set; }

        public string Tipo { get; set; }

        public Estado_Aeronave Destruida { get; set; }

        public Combustible combustible { get; set; }

        public List<Armamento> armamento = new List<Armamento>();
    }



}

感谢阅读。祝你有愉快的一天。

1 个答案:

答案 0 :(得分:3)

armamento需要是一个财产

public List<Armamento> armamento { get; set; }