WPF binding ComboBox to values from two objects












0















Good afternoon,



I have an issue binding two objects to my combobox.



Technically, I'm trying to achieve this:



CrudGrid.SelectionChanged += (o, e) =>
{
CustomersComboBox.SelectedItem = SelectedModel?.Customer;
};


But including all the cool features of TwoWay binding, meaning that changing SelectedItem in ComboBox also changes it on the SelectedModel.



To achieve such thing, I've tried doing this:



<ComboBox x:Name="CustomersComboBox" 
ItemsSource="{Binding Customers}"
DisplayMemberPath="FullName"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=SelectedModel.Customer, Mode=TwoWay}",
SelectedValuePath="FullName"/>


Which didn't work nor display any error in the output console.



It's also worth pointing out that Customers is an observable collection of type 'Customer' whereas SelectedModel is of type 'Contract'.
Contracts have Customer attached to them via Contract.Customer.



I could easily provide this functionality with code behind but that kinda dodges the purpose of MVVM.










share|improve this question























  • Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

    – mobearette
    Nov 22 '18 at 21:21











  • @mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

    – JCode
    Nov 22 '18 at 21:36











  • Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

    – elgonzo
    Nov 22 '18 at 23:01













  • @elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

    – JCode
    Nov 23 '18 at 6:41











  • If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

    – elgonzo
    Nov 23 '18 at 10:49


















0















Good afternoon,



I have an issue binding two objects to my combobox.



Technically, I'm trying to achieve this:



CrudGrid.SelectionChanged += (o, e) =>
{
CustomersComboBox.SelectedItem = SelectedModel?.Customer;
};


But including all the cool features of TwoWay binding, meaning that changing SelectedItem in ComboBox also changes it on the SelectedModel.



To achieve such thing, I've tried doing this:



<ComboBox x:Name="CustomersComboBox" 
ItemsSource="{Binding Customers}"
DisplayMemberPath="FullName"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=SelectedModel.Customer, Mode=TwoWay}",
SelectedValuePath="FullName"/>


Which didn't work nor display any error in the output console.



It's also worth pointing out that Customers is an observable collection of type 'Customer' whereas SelectedModel is of type 'Contract'.
Contracts have Customer attached to them via Contract.Customer.



I could easily provide this functionality with code behind but that kinda dodges the purpose of MVVM.










share|improve this question























  • Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

    – mobearette
    Nov 22 '18 at 21:21











  • @mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

    – JCode
    Nov 22 '18 at 21:36











  • Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

    – elgonzo
    Nov 22 '18 at 23:01













  • @elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

    – JCode
    Nov 23 '18 at 6:41











  • If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

    – elgonzo
    Nov 23 '18 at 10:49
















0












0








0








Good afternoon,



I have an issue binding two objects to my combobox.



Technically, I'm trying to achieve this:



CrudGrid.SelectionChanged += (o, e) =>
{
CustomersComboBox.SelectedItem = SelectedModel?.Customer;
};


But including all the cool features of TwoWay binding, meaning that changing SelectedItem in ComboBox also changes it on the SelectedModel.



To achieve such thing, I've tried doing this:



<ComboBox x:Name="CustomersComboBox" 
ItemsSource="{Binding Customers}"
DisplayMemberPath="FullName"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=SelectedModel.Customer, Mode=TwoWay}",
SelectedValuePath="FullName"/>


Which didn't work nor display any error in the output console.



It's also worth pointing out that Customers is an observable collection of type 'Customer' whereas SelectedModel is of type 'Contract'.
Contracts have Customer attached to them via Contract.Customer.



I could easily provide this functionality with code behind but that kinda dodges the purpose of MVVM.










share|improve this question














Good afternoon,



I have an issue binding two objects to my combobox.



Technically, I'm trying to achieve this:



CrudGrid.SelectionChanged += (o, e) =>
{
CustomersComboBox.SelectedItem = SelectedModel?.Customer;
};


But including all the cool features of TwoWay binding, meaning that changing SelectedItem in ComboBox also changes it on the SelectedModel.



To achieve such thing, I've tried doing this:



<ComboBox x:Name="CustomersComboBox" 
ItemsSource="{Binding Customers}"
DisplayMemberPath="FullName"
IsSynchronizedWithCurrentItem="True"
SelectedValue="{Binding Path=SelectedModel.Customer, Mode=TwoWay}",
SelectedValuePath="FullName"/>


Which didn't work nor display any error in the output console.



It's also worth pointing out that Customers is an observable collection of type 'Customer' whereas SelectedModel is of type 'Contract'.
Contracts have Customer attached to them via Contract.Customer.



I could easily provide this functionality with code behind but that kinda dodges the purpose of MVVM.







c# wpf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 20:56









JCodeJCode

707212




707212













  • Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

    – mobearette
    Nov 22 '18 at 21:21











  • @mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

    – JCode
    Nov 22 '18 at 21:36











  • Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

    – elgonzo
    Nov 22 '18 at 23:01













  • @elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

    – JCode
    Nov 23 '18 at 6:41











  • If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

    – elgonzo
    Nov 23 '18 at 10:49





















  • Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

    – mobearette
    Nov 22 '18 at 21:21











  • @mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

    – JCode
    Nov 22 '18 at 21:36











  • Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

    – elgonzo
    Nov 22 '18 at 23:01













  • @elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

    – JCode
    Nov 23 '18 at 6:41











  • If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

    – elgonzo
    Nov 23 '18 at 10:49



















Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

– mobearette
Nov 22 '18 at 21:21





Could you please specify what do you mean when you say it doesn't work? Does it not set the SelectedModel.Customer to the customer you select from the combobox? Does it throw any runtime exceptions?

– mobearette
Nov 22 '18 at 21:21













@mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

– JCode
Nov 22 '18 at 21:36





@mobearette it shows no errors in the Error List nor Output - the selected item just stays blank until I manually select something.

– JCode
Nov 22 '18 at 21:36













Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

– elgonzo
Nov 22 '18 at 23:01







Try binding against SelectedItem instead of SelectedValue. Note that you use SelectedValuePath="FullName", which means that SelectedValue would work with string values (i am assuming FullName is a string property) and not with Customer objects.

– elgonzo
Nov 22 '18 at 23:01















@elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

– JCode
Nov 23 '18 at 6:41





@elgonzo I've also tried binding SelectedItem aswell as creating an 'adapter' - a simple property called SelectedCustomer that returns in getter => SelectedModel.Customer' - no avail.

– JCode
Nov 23 '18 at 6:41













If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

– elgonzo
Nov 23 '18 at 10:49







If you use SelectedItem, make sure to not use SelectedValueSelectedValuePath. Make sure that the binding can actually find the SelectedCustomer property (it should probably be in the same viewmodel as the Customers collection property). Make sure NotifyPropertyChanged event is raised whenever the value represented by SelectedCustomer changes. Set breakpoints on the setter and getter of the SelectedCustomer property to verify that the binding mechanism is actually being able to access the property.

– elgonzo
Nov 23 '18 at 10:49














1 Answer
1






active

oldest

votes


















0














Have you tried to RaisePropertyChanged?



RaisePropertyChanged(SelectedModel.Customer); to notify the UI that something has been changed in the model. Perhaps implement it upon the selection change. That way it will fire up the property bound to your view.



public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}


then



CrudGrid.SelectionChanged += (o, e) =>
{
CustomersComboBox.SelectedItem = SelectedModel?.Customer;
RaisePropertyChanged(nameof(SelectedModel.Customer));

};





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437887%2fwpf-binding-combobox-to-values-from-two-objects%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Have you tried to RaisePropertyChanged?



    RaisePropertyChanged(SelectedModel.Customer); to notify the UI that something has been changed in the model. Perhaps implement it upon the selection change. That way it will fire up the property bound to your view.



    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
    if (this.PropertyChanged != null)
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


    then



    CrudGrid.SelectionChanged += (o, e) =>
    {
    CustomersComboBox.SelectedItem = SelectedModel?.Customer;
    RaisePropertyChanged(nameof(SelectedModel.Customer));

    };





    share|improve this answer




























      0














      Have you tried to RaisePropertyChanged?



      RaisePropertyChanged(SelectedModel.Customer); to notify the UI that something has been changed in the model. Perhaps implement it upon the selection change. That way it will fire up the property bound to your view.



      public event PropertyChangedEventHandler PropertyChanged;
      private void RaisePropertyChanged(string propertyName)
      {
      if (this.PropertyChanged != null)
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }


      then



      CrudGrid.SelectionChanged += (o, e) =>
      {
      CustomersComboBox.SelectedItem = SelectedModel?.Customer;
      RaisePropertyChanged(nameof(SelectedModel.Customer));

      };





      share|improve this answer


























        0












        0








        0







        Have you tried to RaisePropertyChanged?



        RaisePropertyChanged(SelectedModel.Customer); to notify the UI that something has been changed in the model. Perhaps implement it upon the selection change. That way it will fire up the property bound to your view.



        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
        if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        then



        CrudGrid.SelectionChanged += (o, e) =>
        {
        CustomersComboBox.SelectedItem = SelectedModel?.Customer;
        RaisePropertyChanged(nameof(SelectedModel.Customer));

        };





        share|improve this answer













        Have you tried to RaisePropertyChanged?



        RaisePropertyChanged(SelectedModel.Customer); to notify the UI that something has been changed in the model. Perhaps implement it upon the selection change. That way it will fire up the property bound to your view.



        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
        if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        then



        CrudGrid.SelectionChanged += (o, e) =>
        {
        CustomersComboBox.SelectedItem = SelectedModel?.Customer;
        RaisePropertyChanged(nameof(SelectedModel.Customer));

        };






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 23:33









        Joseph MerisJoseph Meris

        376




        376
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437887%2fwpf-binding-combobox-to-values-from-two-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini