對不起,我不清楚的問題:我不知道怎麼寫什麼,我想問問,但我的代碼可以幫助我清除它:如何「綁定」知道哪個對象我想要(XAML,DataGridTextColumn)
VehicleWidget.xaml
<local:ParkingWidget x:Class="VehiclesWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Widgets" Loaded="ParkingWidget_Loaded" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="65" Width="420">
<Grid Height="61" Width="411">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"/> <!--0-->
<ColumnDefinition Width="Auto"/> <!--1-->
<ColumnDefinition Width="Auto"/> <!--2-->
<ColumnDefinition Width="298"/>
<ColumnDefinition Width="0*" />
<ColumnDefinition Width="38*" />
<!--3-->
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5"/> <!--0-->
<RowDefinition Height="Auto"/> <!--1-->
<RowDefinition Height="5"/> <!--2-->
<RowDefinition Height="Auto"/> <!--3-->
<RowDefinition Height="5"/> <!--4-->
</Grid.RowDefinitions>
<Label
Content="Vehicles"
Grid.Column="1" Grid.Row="1"/>
<Button
Name="btnRefresh"
Grid.Column="2" Grid.Row="1"
Content="Refresh"
Width="98"
HorizontalAlignment="Right"
Click="btnRefresh_Click" Grid.ColumnSpan="2" Margin="0,2,257,2">
</Button>
<DataGrid
Name="VehiclesDataGrid"
AutoGenerateColumns="False"
IsReadOnly="True"
Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" FontFamily="Arial" Margin="0,0,3,0">
<DataGrid.Columns>
<DataGridTextColumn Header="Time" x:Name="timeColumn" Binding="{Binding Path=Time}" />
<DataGridTextColumn Header="TicketId" x:Name="tickedIdColumn" Binding="{Binding Path=TicketId}" />
<DataGridTextColumn Header="LPN" x:Name="lpnColumn" Binding="{Binding Path=LPN}" Visibility="Hidden" />
<DataGridHyperlinkColumn Header="Picture" x:Name="pictureColumn" Binding="{Binding Path=TicketId}" Visibility="Hidden">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="Hyperlink_Click"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
<DataGridTextColumn Header="Class" x:Name="classColumn" Binding="{Binding Path=HeightClassification}" />
<DataGridTextColumn Header="Weight" x:Name="weightColumn" Binding="{Binding Path=Weight}" />
<DataGridTextColumn Header="Orientation" x:Name="orientationColumn" Binding="{Binding Path=Orientation.Degrees}" />
<DataGridTextColumn Header="Piston1" x:Name="piston1Column" Binding="{Binding Path=Piston1}" />
<DataGridTextColumn Header="Piston2" x:Name="piston2Column" Binding="{Binding Path=Piston2}" />
<DataGridTextColumn Header="Location" x:Name="locationColumn" Binding="{Binding Path=Location}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
在 「VehicleWidget.xaml.cs」 我有 「的車輛列表」,並在 「汽車」 類我的屬性:Piston1,Piston2,大小,LPN和當然重量
以下行,將列添加到網格並將其命名爲「Weight」並將其綁定到Weight。
<DataGridTextColumn Header="Weight" x:Name="weightColumn" Binding="{Binding Path=Weight}" />
綁定知道如何選擇哪個對象,將其作爲屬性?
我問這個問題,因爲我想綁定一個屬性不同的對象(不是Vehicle,就像本例中的NewVehicle,它繼承自Vehicle)。
它在MSDN上的[數據綁定概述](http://msdn.microsoft.com/zh-cn/library/ms752347.aspx)文章中詳細介紹。 – Clemens