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
18/07/2008 10:57:49
 
 
À
17/07/2008 16:04:45
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01331472
Message ID:
01332301
Vues:
6
DUDE!! You are super smart with stuff.

It works perfectly now!

When you said "You can't bind to fields so make this a Property", that was something I did not realize, and it made all the difference in the world. I was driving in my car (and looking at the UT web site on my iPhone) when I saw your reply. I was so wanting to get to my computer to test it, but I could not get to it until this morning.

Thanks to you, I am now doing all binding in XAML, and it *DOES* update the Customer object of the Job based on the Customer object chosen in the ComboBox. No code-behind at all.

I am not yet totally clear on the Property vs. Field thing, so I need to learn when which one to use when. For instance, should the Job object defined in the Window class be a Field or a Property and should it be public or private?


John - I cannot tell you how happy I am to get to this place! I truly say 'Thank you!' for taking so much time with me on this.


Some important things to get this working were:
---------------------------------------------------------------------------
0. Understand that you have to change the Customer object reference on the Job, not the foreign key property cust_num on the Job object
1. Exposing custs as a *PROPERTY* on the Window class so I can bind to it in XAML
2. setting the ComboBox SelectedValue as the Customer object (of the Job Object) not the cust_num
3. Apparently, *NOT* setting a value for SelectedValuePath... it seems to default to the object of the ItemsSource collection, and I guess that is why it works 2-way with SelectedValue (the Customer reference on the Job object)


Here's the final XAML code:
    <ComboBox x:Name="dropdownCustomer" IsSynchronizedWithCurrentItem="True" AllowDrop="True"
              ItemsSource="{Binding Path=custs, ElementName=winJobEdit}"
              DisplayMemberPath="company"
              SelectedValue="{Binding Path=Customer, Mode=TwoWay}"
              />
And the very simple code-behind to support it all
namespace wpf2
{
    public partial class WindowJobEdit : Window
    {
        private string _JobNo;

        DataClasses1DataContext ctx = new DataClasses1DataContext();
        Job JobToEdit;
        public IQueryable<Customer> custs {get; set;}

        public WindowJobEdit(string JobNo)
        {

            custs = from c in ctx.Customers
                         select c;
            
            InitializeComponent();
            
            GetJobToEdit(JobNo);

            gridMain.DataContext = JobToEdit;
  
        }


        public void GetJobToEdit(string JobNo)
        {

            if (JobNo != null)
            {
               _JobNo = JobNo; // Not really sure if I need this or not
                JobToEdit = ctx.Jobs.Single(o => o.job_num == JobNo);
            }
        }
>>Well...The first problem I had was setting the ItemsSource in XAML. I couldn't figure it out, so I do it in the form constrctor. Seeing more of your code, I think I see a slightly diffferent structure...
>
>That does work fine.
>
>>>As far as my ComboBox goes, it is not in a UserControl, it's just a plain UI control in a HeaderedContentControl (I wonder where I learned about that), that is also in a StackPanel, that is also in a Grid. I tried to set ItemsSource="{Binding Path=custs, ElementName=______ (don't know what to put here).
>
>My UserControl was basically the same as your Window, it was the root element of the XAML file.
>
>>So, in code-behind, I set the DataContext of gridMain to a Job, and I also set the ItemsSource on the ComboBox to the full list of Customer objects (the "custs" collection). I really want to figure out setting the ItemsSource in XAML, but haven't so far.
>
>You can't bind to fields so make this a Property:
public IQueryable<Customer> custs { get; set;}
>Substitute Window for UserControl:
<Window ... Name="winJobEdit"> ...
>Note the element name being used in the binding here:
ItemsSource="{Binding Path=custs, ElementName=winJobEdit}"
>
>>However, that is not really the issue with the greater goal here of updating the Customer reference in a Job from the XAML binding, so I've just been letting the ItemsSource thing slide for now. But I'm certainly curious to figure it out.
>
>Try the code above.
>
>>Also, is your DataStores a collection of strings? I notice you are not having to use DisplayMemberValue and SelectedItemPath. My ItemsSource is a collection of Customer objects, so I have to specify which field on the Customer object is what part. I see that you are using SelectedValue but not the other two.
>
>Datastores is a collection of objects. I'm using a DataTemplate instead, but I think either way should work.
<ComboBox.ItemTemplate>
>  <DataTemplate>
>    <TextBlock Text="{Binding Path=company}"/>
>  </DataTemplate>
></ComboBox.ItemTemplate>
>
>>So you are able to point the ItemsSource to a Path on your UserControl, but what do I need to point to in order to get at the public custs collection?
>
>See above, you can't bind to a field so you have to make it a property.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform