0

对不起,不清楚的问题:我不知道如何写我想问的,但我的代码帮助我清除它:

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,当然还有重量

以下行,将列添加到网格并将其命名为“权重”并绑定到权重。

<DataGridTextColumn Header="Weight" x:Name="weightColumn" Binding="{Binding Path=Weight}" />

绑定如何知道选择哪个对象,获取它的属性?

我问这个问题是因为我想绑定不同对象的属性(不是像本例中那样的 Vehicle,而是继承自 Vehicle 的 NewVehicle)。

4

1 回答 1

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 如何知道必须使用哪个对象?

于 2013-08-01T07:22:11.027 回答