locked
How to Bind from Code behind? RRS feed

  • Question

  • Hello msdn

                How can I pass a command parameter from code behind , 

    I have a button whose IsEnabled property is bound to a property of another control,say CheckBox's IsChecked property. how is it possible?

    Button b = new Button(){IsEnabled= //need binding here }


    If you find any Question/Discussion/Answers helpful, Kindly Vote As Helpful Regards Salam

    Wednesday, June 12, 2013 6:12 AM

Answers

  • Due to the lack of information I have come up with the following.

    Binding b = new Binding("IsEnabled");
    b.Mode = BindingMode.OneWay;
    Binding bt = new Binding("TextValue");
    bt.Mode = BindingMode.TwoWay;
    bt.Source = this.ViewModel;
    
    this.btnTester.SetBinding(IsEnabledProperty, b);
    this.btnTester.Command = this.ViewModel.TestCommand;
    this.btnTester.CommandParameter = bt;
    

    I had a few issues with the command parameter but I did get it to work but not without tweaking. Anyways I don't recommend binding from the code behind but this is how you dot it above.

    Thursday, June 13, 2013 12:51 AM

All replies

  •  <Grid x:Name="LayoutRoot" Background="White">
            <CheckBox x:Name="ChkBx" IsChecked=""></CheckBox>
            <Button IsEnabled="{Binding ElementName=ChkBx,Path=IsChecked,Mode=TwoWay}"></Button>
        </Grid>

    find above code which explains binding depends upon other control.

    let me know if you need more help.


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, June 12, 2013 2:52 PM
  • Due to the lack of information I have come up with the following.

    Binding b = new Binding("IsEnabled");
    b.Mode = BindingMode.OneWay;
    Binding bt = new Binding("TextValue");
    bt.Mode = BindingMode.TwoWay;
    bt.Source = this.ViewModel;
    
    this.btnTester.SetBinding(IsEnabledProperty, b);
    this.btnTester.Command = this.ViewModel.TestCommand;
    this.btnTester.CommandParameter = bt;
    

    I had a few issues with the command parameter but I did get it to work but not without tweaking. Anyways I don't recommend binding from the code behind but this is how you dot it above.

    Thursday, June 13, 2013 12:51 AM
  • Hello Syed Amjad,

                  This is possible from xaml side.. I want the same way as I was explained.code behind means from C# code, Not Xaml

    Thanks 


    If you find any Question/Discussion/Answers helpful, Kindly Vote As Helpful Regards Salam

    Thursday, June 13, 2013 4:23 AM
  • Hello Charliet123

                  is it possible to write the code as I mentioned the format?

    like Button b = new Button(){IsEnabled= new Binding(){Path= new PropertyPath("IsChecked"), Mode= BindingMode.OneWay, ElementName= "checkBox1"} };

    ??????

    Regards

    salam


    If you find any Question/Discussion/Answers helpful, Kindly Vote As Helpful Regards Salam

    Thursday, June 13, 2013 4:32 AM