【问题标题】:WPF Binding a List<T> to a ComboBoxWPF 将 List<T> 绑定到 ComboBox
【发布时间】:2015-09-08 08:46:17
【问题描述】:

所以我有这个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();
    }
}

我想将Worker 对象绑定到我所做的一些TextBoxes,但是我需要将Job 对象的List(或数组)绑定到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>

  • 编辑:
    找到了某种解决方案,但我仍然不明白它是如何工作的。我刚刚将我的List 转移到Worker 对象中。我认为问题出在DataContext,因为我一次只能设置一个(目前在Worker)。
  • 仍在寻找答案...

【问题讨论】:

  • 通知用户界面?实现 inotifypropertychanged 或尝试使用 observablecollection 并向其中添加项目
  • @Muds 你能否发布一个答案,举例说明如何做到这一点并接近我的情况?
  • @Muds ? (还有 7 个……)
  • 好吧,我忽略了 GazTheDestroyer 提到的事实,您需要先指出正确的 DataContext ..

标签: c# wpf xaml data-binding combobox


【解决方案1】:

您已将视图的DataContext 设置为Worker 的实例,但您的List 属性在视图上。

您可以使用RelativeSource 指向该绑定的视图。

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

或者ElementName,如果你在窗口上放了一个名字:

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

如果您在绑定方面遇到问题,请查看您的 DevStudio 输出窗口,因为绑定错误会显示在那里,并且通常会为您提供有关问题所在的线索。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2010-12-09
    相关资源
    最近更新 更多