Binding of a view variable to a UserControl ViewModel declared inside the view











up vote
-1
down vote

favorite












I am stuck in this problem since a few days, can't solve it :



I have some views V1, V2, ..., with view-models V1vm, V2vm,..., each using a UserControl MyUC, with view-model MyUCvm. UserControl MyUC depends on a bool attached property myParam. Let's say view V1 defines the value through a CheckBox. I can bind myParam to the CheckBox.IsChecked in V1 XAML :



<UserControl x:Class="V1" ...>
...
<CheckBox x:Name="cb" />
<MyUC local:MyUC.myParam="{Binding ElementName=cb, Path=IsChecked}" />
</UserControl>


In view-model MyUCvm I declare a property :



public class MyUCvm : ViewModelBase
{
private bool _condition;
public bool Condition
{
get { return condition; }
set
{
_condition = value;
Notify("Condition");
}
}
...
}


I want Condition to be bound to myParam so that what is displayed by MyUC in View V1 depends on the checkbox cb (and other patterns for the other views).



Maybe I could code in V1vm an update of MyUCvm in C#. But I believe this is not the right way.



It seams to me that it is more "pure" to bind myParam in MyUC XAML to Condition, with some kind of (not working) code :



<UserControl x:Class="MyUC" ...>
...
<WHAT_TO_PUT_HERE? local:MyUC.myParam="{Binding Condition, Mode=TwoWay}" />
...
</UserControl>


Am I targeting the right solution ?
How to acheive this ?










share|improve this question
























  • Why don't you bind the CheckBox to Condition?
    – mm8
    Sep 28 at 11:33















up vote
-1
down vote

favorite












I am stuck in this problem since a few days, can't solve it :



I have some views V1, V2, ..., with view-models V1vm, V2vm,..., each using a UserControl MyUC, with view-model MyUCvm. UserControl MyUC depends on a bool attached property myParam. Let's say view V1 defines the value through a CheckBox. I can bind myParam to the CheckBox.IsChecked in V1 XAML :



<UserControl x:Class="V1" ...>
...
<CheckBox x:Name="cb" />
<MyUC local:MyUC.myParam="{Binding ElementName=cb, Path=IsChecked}" />
</UserControl>


In view-model MyUCvm I declare a property :



public class MyUCvm : ViewModelBase
{
private bool _condition;
public bool Condition
{
get { return condition; }
set
{
_condition = value;
Notify("Condition");
}
}
...
}


I want Condition to be bound to myParam so that what is displayed by MyUC in View V1 depends on the checkbox cb (and other patterns for the other views).



Maybe I could code in V1vm an update of MyUCvm in C#. But I believe this is not the right way.



It seams to me that it is more "pure" to bind myParam in MyUC XAML to Condition, with some kind of (not working) code :



<UserControl x:Class="MyUC" ...>
...
<WHAT_TO_PUT_HERE? local:MyUC.myParam="{Binding Condition, Mode=TwoWay}" />
...
</UserControl>


Am I targeting the right solution ?
How to acheive this ?










share|improve this question
























  • Why don't you bind the CheckBox to Condition?
    – mm8
    Sep 28 at 11:33













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am stuck in this problem since a few days, can't solve it :



I have some views V1, V2, ..., with view-models V1vm, V2vm,..., each using a UserControl MyUC, with view-model MyUCvm. UserControl MyUC depends on a bool attached property myParam. Let's say view V1 defines the value through a CheckBox. I can bind myParam to the CheckBox.IsChecked in V1 XAML :



<UserControl x:Class="V1" ...>
...
<CheckBox x:Name="cb" />
<MyUC local:MyUC.myParam="{Binding ElementName=cb, Path=IsChecked}" />
</UserControl>


In view-model MyUCvm I declare a property :



public class MyUCvm : ViewModelBase
{
private bool _condition;
public bool Condition
{
get { return condition; }
set
{
_condition = value;
Notify("Condition");
}
}
...
}


I want Condition to be bound to myParam so that what is displayed by MyUC in View V1 depends on the checkbox cb (and other patterns for the other views).



Maybe I could code in V1vm an update of MyUCvm in C#. But I believe this is not the right way.



It seams to me that it is more "pure" to bind myParam in MyUC XAML to Condition, with some kind of (not working) code :



<UserControl x:Class="MyUC" ...>
...
<WHAT_TO_PUT_HERE? local:MyUC.myParam="{Binding Condition, Mode=TwoWay}" />
...
</UserControl>


Am I targeting the right solution ?
How to acheive this ?










share|improve this question















I am stuck in this problem since a few days, can't solve it :



I have some views V1, V2, ..., with view-models V1vm, V2vm,..., each using a UserControl MyUC, with view-model MyUCvm. UserControl MyUC depends on a bool attached property myParam. Let's say view V1 defines the value through a CheckBox. I can bind myParam to the CheckBox.IsChecked in V1 XAML :



<UserControl x:Class="V1" ...>
...
<CheckBox x:Name="cb" />
<MyUC local:MyUC.myParam="{Binding ElementName=cb, Path=IsChecked}" />
</UserControl>


In view-model MyUCvm I declare a property :



public class MyUCvm : ViewModelBase
{
private bool _condition;
public bool Condition
{
get { return condition; }
set
{
_condition = value;
Notify("Condition");
}
}
...
}


I want Condition to be bound to myParam so that what is displayed by MyUC in View V1 depends on the checkbox cb (and other patterns for the other views).



Maybe I could code in V1vm an update of MyUCvm in C#. But I believe this is not the right way.



It seams to me that it is more "pure" to bind myParam in MyUC XAML to Condition, with some kind of (not working) code :



<UserControl x:Class="MyUC" ...>
...
<WHAT_TO_PUT_HERE? local:MyUC.myParam="{Binding Condition, Mode=TwoWay}" />
...
</UserControl>


Am I targeting the right solution ?
How to acheive this ?







c# wpf xaml data-binding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 27 at 15:41

























asked Sep 27 at 15:20









Sylvain B.

309214




309214












  • Why don't you bind the CheckBox to Condition?
    – mm8
    Sep 28 at 11:33


















  • Why don't you bind the CheckBox to Condition?
    – mm8
    Sep 28 at 11:33
















Why don't you bind the CheckBox to Condition?
– mm8
Sep 28 at 11:33




Why don't you bind the CheckBox to Condition?
– mm8
Sep 28 at 11:33












2 Answers
2






active

oldest

votes

















up vote
0
down vote













When you need one custom control to bind to the properties of another control, you best solution is going to be to register dependency properties in the code behind of the custom control who has bound properties that need exposed.



You can certainly still get and set your viewModel bindable properties from within the get and set of your registered dependency property. However, that is going to be your best bet.



So make a DependencyProperty for your type of controller, give it a name, and register the onChanged if you care to monitor it in code. Then create your get and set properties for it to point the dependency property to.



From their, compile and reopen the xaml file that is trying to utilize it, and the property will now be there in auto-fill intellisense for you to bind to.



Happy Coding.






share|improve this answer




























    up vote
    0
    down vote













    Actually instead of twisting everything around, I have chosen to use simple ViewModel logic, which is much simpler.



    In ViewModels V1vmand V2vm I define a property of type MyUCvm that I use to define the DataContext of the <local:MyUC DataContext={Binding RefToMyUCvm} ... /> xaml element in views V1and V2.



    In V1vmand V2vm I use a bool property MyParam to bind to the CheckBox.IsChecked (or something else ...). And I do the work in getter/setter of the property.



    public class V1vm : ViewModelBase
    {
    //...

    public MyUCvm RefToUCvm { get; set; }

    public bool MyParam
    {
    get { return MyUCvm?.MyParam ?? false; }
    set
    {
    if (MyUCvm != null)
    {
    MyUCvm.MyParam = value;
    NotifyPropertyChanged("MyParam");
    }
    }
    }
    }


    This is much straitforward and simple...






    share|improve this answer





















    • Thanks to Sam and mm8 for helping me to find a simple solution
      – Sylvain B.
      Nov 22 at 12:42













    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',
    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%2f52540296%2fbinding-of-a-view-variable-to-a-usercontrol-viewmodel-declared-inside-the-view%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    When you need one custom control to bind to the properties of another control, you best solution is going to be to register dependency properties in the code behind of the custom control who has bound properties that need exposed.



    You can certainly still get and set your viewModel bindable properties from within the get and set of your registered dependency property. However, that is going to be your best bet.



    So make a DependencyProperty for your type of controller, give it a name, and register the onChanged if you care to monitor it in code. Then create your get and set properties for it to point the dependency property to.



    From their, compile and reopen the xaml file that is trying to utilize it, and the property will now be there in auto-fill intellisense for you to bind to.



    Happy Coding.






    share|improve this answer

























      up vote
      0
      down vote













      When you need one custom control to bind to the properties of another control, you best solution is going to be to register dependency properties in the code behind of the custom control who has bound properties that need exposed.



      You can certainly still get and set your viewModel bindable properties from within the get and set of your registered dependency property. However, that is going to be your best bet.



      So make a DependencyProperty for your type of controller, give it a name, and register the onChanged if you care to monitor it in code. Then create your get and set properties for it to point the dependency property to.



      From their, compile and reopen the xaml file that is trying to utilize it, and the property will now be there in auto-fill intellisense for you to bind to.



      Happy Coding.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        When you need one custom control to bind to the properties of another control, you best solution is going to be to register dependency properties in the code behind of the custom control who has bound properties that need exposed.



        You can certainly still get and set your viewModel bindable properties from within the get and set of your registered dependency property. However, that is going to be your best bet.



        So make a DependencyProperty for your type of controller, give it a name, and register the onChanged if you care to monitor it in code. Then create your get and set properties for it to point the dependency property to.



        From their, compile and reopen the xaml file that is trying to utilize it, and the property will now be there in auto-fill intellisense for you to bind to.



        Happy Coding.






        share|improve this answer












        When you need one custom control to bind to the properties of another control, you best solution is going to be to register dependency properties in the code behind of the custom control who has bound properties that need exposed.



        You can certainly still get and set your viewModel bindable properties from within the get and set of your registered dependency property. However, that is going to be your best bet.



        So make a DependencyProperty for your type of controller, give it a name, and register the onChanged if you care to monitor it in code. Then create your get and set properties for it to point the dependency property to.



        From their, compile and reopen the xaml file that is trying to utilize it, and the property will now be there in auto-fill intellisense for you to bind to.



        Happy Coding.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 27 at 16:55









        Sam

        2,93611022




        2,93611022
























            up vote
            0
            down vote













            Actually instead of twisting everything around, I have chosen to use simple ViewModel logic, which is much simpler.



            In ViewModels V1vmand V2vm I define a property of type MyUCvm that I use to define the DataContext of the <local:MyUC DataContext={Binding RefToMyUCvm} ... /> xaml element in views V1and V2.



            In V1vmand V2vm I use a bool property MyParam to bind to the CheckBox.IsChecked (or something else ...). And I do the work in getter/setter of the property.



            public class V1vm : ViewModelBase
            {
            //...

            public MyUCvm RefToUCvm { get; set; }

            public bool MyParam
            {
            get { return MyUCvm?.MyParam ?? false; }
            set
            {
            if (MyUCvm != null)
            {
            MyUCvm.MyParam = value;
            NotifyPropertyChanged("MyParam");
            }
            }
            }
            }


            This is much straitforward and simple...






            share|improve this answer





















            • Thanks to Sam and mm8 for helping me to find a simple solution
              – Sylvain B.
              Nov 22 at 12:42

















            up vote
            0
            down vote













            Actually instead of twisting everything around, I have chosen to use simple ViewModel logic, which is much simpler.



            In ViewModels V1vmand V2vm I define a property of type MyUCvm that I use to define the DataContext of the <local:MyUC DataContext={Binding RefToMyUCvm} ... /> xaml element in views V1and V2.



            In V1vmand V2vm I use a bool property MyParam to bind to the CheckBox.IsChecked (or something else ...). And I do the work in getter/setter of the property.



            public class V1vm : ViewModelBase
            {
            //...

            public MyUCvm RefToUCvm { get; set; }

            public bool MyParam
            {
            get { return MyUCvm?.MyParam ?? false; }
            set
            {
            if (MyUCvm != null)
            {
            MyUCvm.MyParam = value;
            NotifyPropertyChanged("MyParam");
            }
            }
            }
            }


            This is much straitforward and simple...






            share|improve this answer





















            • Thanks to Sam and mm8 for helping me to find a simple solution
              – Sylvain B.
              Nov 22 at 12:42















            up vote
            0
            down vote










            up vote
            0
            down vote









            Actually instead of twisting everything around, I have chosen to use simple ViewModel logic, which is much simpler.



            In ViewModels V1vmand V2vm I define a property of type MyUCvm that I use to define the DataContext of the <local:MyUC DataContext={Binding RefToMyUCvm} ... /> xaml element in views V1and V2.



            In V1vmand V2vm I use a bool property MyParam to bind to the CheckBox.IsChecked (or something else ...). And I do the work in getter/setter of the property.



            public class V1vm : ViewModelBase
            {
            //...

            public MyUCvm RefToUCvm { get; set; }

            public bool MyParam
            {
            get { return MyUCvm?.MyParam ?? false; }
            set
            {
            if (MyUCvm != null)
            {
            MyUCvm.MyParam = value;
            NotifyPropertyChanged("MyParam");
            }
            }
            }
            }


            This is much straitforward and simple...






            share|improve this answer












            Actually instead of twisting everything around, I have chosen to use simple ViewModel logic, which is much simpler.



            In ViewModels V1vmand V2vm I define a property of type MyUCvm that I use to define the DataContext of the <local:MyUC DataContext={Binding RefToMyUCvm} ... /> xaml element in views V1and V2.



            In V1vmand V2vm I use a bool property MyParam to bind to the CheckBox.IsChecked (or something else ...). And I do the work in getter/setter of the property.



            public class V1vm : ViewModelBase
            {
            //...

            public MyUCvm RefToUCvm { get; set; }

            public bool MyParam
            {
            get { return MyUCvm?.MyParam ?? false; }
            set
            {
            if (MyUCvm != null)
            {
            MyUCvm.MyParam = value;
            NotifyPropertyChanged("MyParam");
            }
            }
            }
            }


            This is much straitforward and simple...







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 12:41









            Sylvain B.

            309214




            309214












            • Thanks to Sam and mm8 for helping me to find a simple solution
              – Sylvain B.
              Nov 22 at 12:42




















            • Thanks to Sam and mm8 for helping me to find a simple solution
              – Sylvain B.
              Nov 22 at 12:42


















            Thanks to Sam and mm8 for helping me to find a simple solution
            – Sylvain B.
            Nov 22 at 12:42






            Thanks to Sam and mm8 for helping me to find a simple solution
            – Sylvain B.
            Nov 22 at 12:42




















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f52540296%2fbinding-of-a-view-variable-to-a-usercontrol-viewmodel-declared-inside-the-view%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







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)