简体   繁体   English

WPF绑定列表 <T> 到一个组合框

[英]WPF Binding a List<T> to a ComboBox

So I have this Window class: 所以我有这个Window类:

public partial class WorkerCard : Window
{
    public string jobsPath;
    public List<Job> List { get; set; }

    public WorkerCard()
    {
        InitializeComponent();

        Worker w = new Worker
        {
            Num = 1,
            Id = 123456789,
            Name = "ישראל",
            Last = "ישראלי",
            City = "",
            WorkStart = new DateTime(2015, 10, 1),
            WorkEnd = new DateTime(2017, 1, 1),
            Job = new Job
            {
                Name = "",
                Rate = 0,
                Global = 0
            },
            Rides = 0,
            Ride = false
        };
        this.DataContext = w;

        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ShalomCollege\\";
        Job[] jobs = new JavaScriptSerializer().Deserialize<Job[]>(File.ReadAllText(path + "jobs.json"));
        List = jobs.OfType<Job>().ToList();
    }
}

I wanted to bind the Worker object to some TextBox es which I did, but then I needed to bind a List (or an array) of Job objects to a ComboBox . 我想将Worker对象绑定到我做过的一些TextBox ,但是随后我需要将Job对象的List (或数组)绑定到ComboBox Nothing seems to work. 似乎没有任何作用。

XAML (separated the ComboBox ): XAML(用ComboBox分隔):

<Window x:Class="ShalomColl.WorkerCard"
    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:ShalomColl"
    mc:Ignorable="d"
    Title="WorkerCard" Height="350" Width="350">
<Window.Resources>
    <Style x:Key="IsZero" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Job.Rate}" Value="0">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid FlowDirection="RightToLeft">
    <StackPanel x:Name="stackPanel">
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="ת.ז:" />
            <TextBox Text="{Binding Id}" MaxLength="9" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="שם פרטי:" />
            <TextBox Text="{Binding Name}" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="שם משפחה:" />
            <TextBox Text="{Binding Last}" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="תחילת עבודה:" />
            <DatePicker Text="{Binding WorkStart}" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="סיום עבודה:" />
            <DatePicker Text="{Binding WorkEnd}" />

        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="תפקיד:" />
            <ComboBox ItemsSource="{Binding List}" />
        </DockPanel>

        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="יישוב:" />
            <ComboBox x:Name="City" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="תעריף לשעה:" />
            <TextBox Text="{Binding Job.Rate}" Style="{StaticResource IsZero}" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <Label DockPanel.Dock="Left" Content="משכורת גלובאלית:" />
            <TextBox Text="{Binding Job.Global}" Style="{StaticResource IsZero}" />
        </DockPanel>
        <DockPanel LastChildFill="True">
            <CheckBox x:Name="chkBox" VerticalAlignment="Center" DockPanel.Dock="Left" IsChecked="{Binding Ride}" />
            <Label DockPanel.Dock="Left" Content="נסיעות:" />
            <TextBox Text="{Binding Rides}" IsEnabled="{Binding ElementName=chkBox, Path=IsChecked}" />
        </DockPanel>
    </StackPanel>
    <Button Content="שלח" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20" Width="70" Click="Submit" />
</Grid>

  • EDIT: 编辑:
    Found some sort of solution but I still don't get how it works. 找到了某种解决方案,但我仍然不知道它是如何工作的。 I just transferred my List into the Worker object. 我刚刚将List传输到Worker对象。 I assume the problem is with the DataContext because I can set only one at a time (which is currently on Worker ). 我认为问题出在DataContext因为我一次只能设置一个(当前在Worker )。
  • Still looking for an answer though... 仍在寻找答案...

You have set the DataContext of your view to an instance of Worker , but your List property is on the view. 您已经将视图的DataContext设置为Worker的实例,但是List属性在视图上。

You can use RelativeSource to point back the view for that one binding. 您可以使用RelativeSource指向该绑定的视图。

<ComboBox ItemsSource="{Binding List, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />

Or ElementName if you put a name on the window: 如果在窗口上放置名称,则为ElementName

<ComboBox ItemsSource="{Binding List, ElementName=root" />

If you have problems with bindings then take a look in your DevStudio output window, as binding errors will show up there and will generally give you a clue as to what's going wrong. 如果您在绑定方面遇到问题,请在DevStudio输出窗口中查看,因为那里会出现绑定错误,并且通常会为您提供出问题的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM