簡體   English   中英

將標簽綁定到WPF C#中的所選項目

[英]Bind Labels to selected item in WPF C#

我的問題可能已經回答了,但是我想看看是否可以根據自己的情況解決該問題。

問題是我有一個包含汽車的ListBox,如果您檢查代碼,就會看到其中有帶有標簽的GridView,這些標簽將獲得第一項(第一輛汽車),但我希望它獲得您在其中選擇的汽車列表框。

如我所說,可能已經有一個尚未找到的答案,但是任何幫助將不勝感激!

<Window x:Class="WBS.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WBS"
        mc:Ignorable="d" x:Name="mainWindow"
        Title="MainWindow" Height="353.2" Width="696.2">
    <Grid Margin="0,0,0,0" Background="#FFDFE4E3">
        <Button x:Name="btInfo" Content="Filter" HorizontalAlignment="Left" Margin="336,132,0,0" VerticalAlignment="Top" Width="75" Click="btInfo_Click"/>
        <Button x:Name="btEditCar" Content="Bewerk" HorizontalAlignment="Left" Margin="336,92,0,0" VerticalAlignment="Top" Width="75" Click="btEditCar_Click"/>
        <ListBox x:Name="lbCars" HorizontalAlignment="Left" Height="140" Margin="184,50,0,0" VerticalAlignment="Top" Width="142"/>
        <Button x:Name="btAddCar" Content="Voeg toe" HorizontalAlignment="Left" Margin="336,50,0,0" VerticalAlignment="Top" Width="75" Click="btAddCar_Click"/>
        <ListBox x:Name="lbFleets" HorizontalAlignment="Left" Height="140" Margin="10,50,0,0" VerticalAlignment="Top" Width="144" SelectionChanged="lbFleets_SelectionChanged"/>

        <Grid Margin="515,50,9.6,53.6" Name="grdCarOverview" Background="#FF91908F">
            <Label x:Name="lbBrand" Content="{Binding Path=[0].Brand}" HorizontalAlignment="Left" Margin="10,42,0,0" VerticalAlignment="Top" Height="27" Width="146" BorderBrush="#FFB3B3B3" BorderThickness="1"/>
            <Label x:Name="lbColor" Content="{Binding Path=[0].Color}" HorizontalAlignment="Left" Margin="10,74,0,0" VerticalAlignment="Top" Height="30" Width="146" BorderThickness="1" BorderBrush="#FFB3B3B3"/>
            <Label x:Name="lbConstructionYear" Content="{Binding Path=[0].ConstructionYear}" HorizontalAlignment="Left" Margin="10,112,0,0" VerticalAlignment="Top" Height="85" Width="146" FontSize="36" FontWeight="Bold" BorderBrush="#FFB3B3B3" BorderThickness="1"/>
            <Label Content="De eerste auto:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
        </Grid>
        <Label Content="Lijst met vloten:" HorizontalAlignment="Left" Margin="10,19,0,0" VerticalAlignment="Top" Width="110"/>
        <Label Content="Lijst auto's per vloot:" HorizontalAlignment="Left" Margin="184,19,0,0" VerticalAlignment="Top"/>
        <Button x:Name="btAddFleet" Content="Voeg toe" HorizontalAlignment="Left" Margin="36,206,0,0" VerticalAlignment="Top" Width="75" Click="btAddFleet_Click"/>
        <Button x:Name="btEditFleet" Content="Bewerk" HorizontalAlignment="Left" Margin="36,242,0,0" VerticalAlignment="Top" Width="75" Click="btEditFleet_Click"/>
        <Button x:Name="btRemoveCar" Content="Verwijder" HorizontalAlignment="Left" Margin="336,171,0,0" VerticalAlignment="Top" Width="75" Click="btRemoveCar_Click"/>
        <Button x:Name="btRemoveFleet" Content="Verwijder" HorizontalAlignment="Left" Margin="36,277,0,0" VerticalAlignment="Top" Width="75" Click="btRemoveFleet_Click"/>
    </Grid>
</Window>

CS:

public ObservableCollection<Car> Cars { get; set; }
public Car SelectedCar { get; set; }
public ObservableCollection<Fleet> Fleets { get; set; }

您需要向ListBox控件添加綁定

    <ListBox x:Name="lbCars" 
             ItemsSource="{Binding Cars }"
             SelectedValue="{Binding SelectedCarId}" 
             SelectedValuePath="Id" DisplayMemberPath="Name" 
             HorizontalAlignment="Left" 
             Height="140" Margin="184,50,0,0"
             VerticalAlignment="Top" Width="142"/>

在CS文件中,制作偽數據

 public ObservableCollection<Car> Cars = new ObservableCollection<Car>
 {
     new Car{Id = 0, Name = "Audi"},
     new Car{Id = 1, Name = "Honda"},
     new Car{Id = 2, Name = "Toyota"},
 };

 public int SelectedCarId { get; set; }

SelectedCarId將保存SelectedCarId的汽車ID。 lbFleets ListBox也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM