1

So I have this Window class:

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 TextBoxes which I did, but then I needed to bind a List (or an array) of Job objects to a ComboBox. Nothing seems to work.

XAML (separated the 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. I assume the problem is with the DataContext because I can set only one at a time (which is currently on Worker).
  • Still looking for an answer though...
4
  • 1
    notify UI ? implement inotifypropertychanged or try using observablecollection and add items to it Commented Sep 8, 2015 at 8:48
  • @Muds Could you please post an answer that gives an example on how to do that and close to my situation? Commented Sep 8, 2015 at 9:03
  • @Muds ? (7 more to go...) Commented Sep 8, 2015 at 10:35
  • well I overlooked the fact that GazTheDestroyer has mentioned, you need to point to correct DataContext first .. Commented Sep 8, 2015 at 11:06

1 Answer 1

1

You have set the DataContext of your view to an instance of Worker, but your List property is on the view.

You can use RelativeSource to point back the view for that one binding.

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

Or ElementName if you put a name on the window:

<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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.