对于不明确的问题感到抱歉:我不知道如何写出我想问的内容,但我的代码可以帮助我清除它:
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,而是继承自Vehicle的NewVehicle)。
答案 0 :(得分:0)
XAML中的绑定是变量或对象与xaml小部件(如textview,gridviews等)之间的“链接”。有很多方法可以使用它。我会举几个例子:
-1-源代码中的属性绑定到文本块。当变量更改其值时,文本块将更新:
<TextBlock Text="{Binding SomeProperty}"/>
-2-更复杂的例子,如果变量发生变化,文本框的值会更新,当文本框的值发生变化时变量会更新:
<TextBlock Text="{Binding SomeVar, Mode=TwoWay}"/>
-3-使用转换器进行数据绑定意味着您可以在c#中编写一个转换器来处理任何绑定值并返回任何自定义值/对象:
XAML
<TextBlock Text="TrafficLight" Background="{Binding TrafficLightState, Converter={StaticRessource TrafficLightStateToColorConverter}}">
C#
public class TrafficLightStateToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if(value.Equals("Green"))
return Color.FromKnownColor(KnownColor.Green)
else if(value.Equals("Yellow"))
return Color.FromKnownColor(KnownColor.Yellow)
else
return Color.FromKnownColor(KnownColor.Red)
}
}
这是否回答了您的问题,xaml如何知道必须使用哪个对象?