我如何绑定一个对象的属性,而这个对象已经是另一个已绑定对象的属性

本文关键字:对象 绑定 属性 何绑定 另一个 一个对象 | 更新日期: 2023-09-27 18:10:51

我有一个问题,试图绑定一个对象的属性,这已经是另一个对象的属性。当我绑定上层对象的属性时,一切正常,但当我试图绑定一个属性的属性时,它没有显示任何东西。

我的代码如下: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" Click="lvUsersColumnHeader_Click">Transponder</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="80" DisplayMemberBinding="{Binding Tipo}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Tipo" Click="lvUsersColumnHeader_Click">Tipo</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="110" DisplayMemberBinding="{Binding Destruida}">
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Destruida" Click="lvUsersColumnHeader_Click">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>
                </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;
namespace WpfTutorialSamples.ListView_control
{
    public partial class ListViewColumnSortingSample : Window
    {
        private GridViewColumnHeader listViewSortCol = null;
        private SortAdorner listViewSortAdorner = null;
        public ListViewColumnSortingSample()
        {
            InitializeComponent();
            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} });
            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 } });
            lvUsers.ItemsSource = items;
        }
        private void lvUsersColumnHeader_Click(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader column = (sender as GridViewColumnHeader);
            string sortBy = column.Tag.ToString();
            if (listViewSortCol != null)
            {
                AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
                lvUsers.Items.SortDescriptions.Clear();
            }
            ListSortDirection newDir = ListSortDirection.Ascending;
            if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
                newDir = ListSortDirection.Descending;
            listViewSortCol = column;
            listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
            AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
            lvUsers.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));
        }

        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 int Cargado { get; set; }
        public int 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 class SortAdorner : Adorner
    {
        private static Geometry ascGeometry =
                Geometry.Parse("M 0 4 L 3.5 0 L 7 4 Z");
        private static Geometry descGeometry =
                Geometry.Parse("M 0 0 L 3.5 4 L 7 0 Z");
        public ListSortDirection Direction { get; private set; }
        public SortAdorner(UIElement element, ListSortDirection dir)
            : base(element)
        {
            this.Direction = dir;
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (AdornedElement.RenderSize.Width < 20)
                return;
            TranslateTransform transform = new TranslateTransform
                    (
                            AdornedElement.RenderSize.Width - 15,
                            (AdornedElement.RenderSize.Height - 5) / 2
                    );
            drawingContext.PushTransform(transform);
            Geometry geometry = ascGeometry;
            if (this.Direction == ListSortDirection.Descending)
                geometry = descGeometry;
            drawingContext.DrawGeometry(Brushes.Black, null, geometry);
            drawingContext.Pop();
        }
    }
}
如果你能帮助我,我将永远感激你。

祝你今天愉快。

我如何绑定一个对象的属性,而这个对象已经是另一个已绑定对象的属性

设置DataContext为实际属性名:

<ComboBox  SelectionChanged="ComboBox_StopChanges"  DataContext="{Binding combustible}">

和TextBox绑定到相应的成员:

<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 Path=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 Path=Usado}" VerticalAlignment="Top" />

将以下内容添加到User类:

    public Combustible[] CombustibleArray
    {
        get { return new[] {combustible}; }
    }

然后将ComboBox绑定到combustible,而不是绑定到CombustibleArray,如下所示:

<ComboBox  SelectionChanged="ComboBox_StopChanges" DataContext="{Binding CombustibleArray}">

你的代码不工作的原因是ComboBox数据上下文期望某种数组。它将使用数组中的第一项作为组合框中的第一项。