Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do you bind to a property on a reference object?
Message
De
17/07/2008 13:13:13
 
 
À
17/07/2008 01:04:28
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01331472
Message ID:
01332034
Vues:
8
>Hope I'm not bothering you here

Not at all.

What you learned fits with what I've seen.

My example was working code that I pulled from one of my forms I changed a few things to fit with your dojects. So... the question is why didn't it work?
<ComboBox ItemsSource="{Binding Path=Customers, ElementName=thisControl}"
                       SelectedValue="{Binding Path=Customer, Mode=TwoWay}" >
      <ComboBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=company}"/>
        </DataTemplate>
      </ComboBox.ItemTemplate>
</ComboBox>
From your example ItemsSource does appear to be a collection of Customer objects, so that looks right.

It also looks like your DataContext is set to JobToEdit so that also looks right. Are you getting a binding error in the output window?

Here's the actual code on my system... maybe it will help:

C#
  public partial class cdsJobInfoPage : UserControl
    {
    private cdsJobObject       JobContext;
    public IEnumerable<cdsDataStore> DataStores { get; set; }
    public cdsJobInfoPage(cdsJobObject job)
      {
      JobContext  = job;
      DataContext = job.Entity;  // the entity here is my JobToEdit 
      DataStores  = job.GetDataStores();
      InitializeComponent();
      }
XAML
<UserControl ... Name="ucJobInfo"> ...
    <ComboBox Grid.Row="3" 
              Grid.Column="1" 
              Name="cmbDataStores" 
              Height="20" 
              ItemsSource="{Binding Path=DataStores, ElementName=ucJobInfo}"
              SelectedValue="{Binding Path=cdsDataStore, Mode=TwoWay}" > 
      <ComboBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
      </ComboBox.ItemTemplate>
    </ComboBox>
My jobs are data processing projects so this sets the root of the folder tree where the jobs data files will be stored.

See if you find anything in that. And if you don't let me know exactly what it is doing and not doing.

John

>Hope I'm not bothering you here, but this is a MAJOR hurdle... I'm pulling my hair out over this one!! Spent HOURS today on trying to update the Job.cust_num via pure XAML binding, and I have not accomplished it yet (without using code-behind on SelectionChanged).
>
>Here's a few things I learned (could be wrong on some of this, so please feel free to correct me)
>
>With the Linq-to-Sql modelling and the Association between Job.cust_num and Customer.custno like we are hashing around with here, you CANNOT set the cust_num field of the Job object directly. You get this error in the output window: "A first chance exception of type 'System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException' occurred in wpf2.exe"
>
>And, naturally, the very XAML that I wrote for my UI is causing said error every time I make a selection in the ComboBox, as it is trying set Job.cust_num to the value of the selected item, as the binding instructions have told it to.
>
>
>          <ComboBox x:Name="dropdownCustomer" IsSynchronizedWithCurrentItem="True" AllowDrop="True"
>                    DisplayMemberPath="company" SelectedValuePath="custno"
>                    SelectedValue="{Binding Path=cust_num, Mode=TwoWay}"
>             />
>
>
>
>So, we have already learned that you must change the Customer object within the Job to accomplish what we are after (as far as I have learned from more than one place, this is the way it must be done).
>
>Therefore, I did get it working with the SelectionChanged method and some simple code-behind:
>
>
>          <ComboBox x:Name="dropdownCustomer" IsSynchronizedWithCurrentItem="True" AllowDrop="True"
>                    DisplayMemberPath="company" SelectedValuePath="custno"
>                    SelectedValue="{Binding Path=cust_num, Mode=TwoWay}"
>                    SelectionChanged="dropdownCustomer_SelectionChanged"
>            />
>
>
>Code-behind:
>
>    private void dropdownCustomer_SelectionChanged(object sender, SelectionChangedEventArgs e)
>    {
>        ComboBox SendingObject = (ComboBox)sender;
>
>        if (JobToEdit != null) // must check... is null at form launch
>            JobToEdit.Customer = (Customer)SendingObject.SelectedItem;
>        }
>    }
>
>
>
>By the way, even though the Customer object is from a totally different source context, this does work! (I had heard that objects that are EXACTLY the same are handled this way by the CLR.)
>
>Only thing I still don't like is: Because of the SelectedItem="{Binding Path=cust_num}" in the XAML (which is necessary to point the ComboBox to the correct Customer at the launching of the form), although you do not get a run-time error, and everything in the app works exactly like I want, BUT, if you look at the Output Window it is giving that stupid "A first chance exception of type 'System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException' occurred in wpf2.exe" error with every selection of the ComboBox. I need to figure out how to synch up without SelectedValue.
>
>I have not found one single example on the internet that addresses this matter thoroughly enough to show what to do. Most examples are based on ad-hoc made-on-the-fly object lists that work good for demo, and they sure make it look easy. Let's see 'em do it against real data from a Linq-to-Sql model.
>
>
>
>I did experiment with your suggestion on trying to bind SelectedValue to a Customer reference, but I could not get it wired the right way to make it work.
>
>
>
>
>
>>>Dude, I hope you read this soon!!
>>
>>I did. <g>
>>
>>Your catching up to me I learned about this a few weeks ago. What confuses me is I have changing the ID working fine in one of my classes and I don't know why it is working. <g>
>>
>>What you found was the next thing to try, but let me propose a slightly different solution.
>>
>>
>><ComboBox ItemsSource="{Binding Path=Customers, ElementName=thisControl, Mode=TwoWay}"
>>                       SelectedValue="{Binding Path=Customer, Mode=TwoWay}" >
>>      <ComboBox.ItemTemplate>
>>        <DataTemplate>
>>          <TextBlock Text="{Binding Path=company}"/>
>>        </DataTemplate>
>>      </ComboBox.ItemTemplate>
>></ComboBox>
>>
>>
>>Customers is a list of Customer objects.
>>Customer is Job.Customer (The object not the ID)
>>company is Customer.company
>>
>>Since the selected value is the object not the id you should not need the extension to your class.
>>
>>The one point of uncertainty I have is...
>>
>>If the Customers list is from a different data context than the Job object... does it work?
>>
>>(This was a long post and I tried to answer it quickly so hopefully I understood it all correctly.)
>>
>>John
>>
>>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform