我在尝试绑定对象的属性时遇到问题,该对象已经是另一个对象的属性。当我绑定上层对象的属性时,一切正常,但是当我尝试绑定一个属性的属性时,它不会显示任何内容。
这是我的代码: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();
}
}
}
如果你能提供帮助,我会永远感激你。
度过愉快的一天。
答案 0 :(得分:1)
将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" />
答案 1 :(得分:0)
将以下内容添加到User类:
public Combustible[] CombustibleArray
{
get { return new[] {combustible}; }
}
然后将ComboBox绑定到combustible
,而不是将其绑定到CombustibleArray
,如下所示:
<ComboBox SelectionChanged="ComboBox_StopChanges" DataContext="{Binding CombustibleArray}">
您的代码无法正常工作的原因是ComboBox数据上下文需要某种数组。它将使用数组中的第一项作为ComboBox中的第一项。这正是我建议的修改所做的。