1

How can I create this from code ?

<CheckBox Command="{Binding Source={StaticResource VMLocator}, Path=TimeTableInformationViewModel.MyCommand }"
                  CommandParameter="{Binding valueFromInput}" />

I am not sure how to set Command property from behind code :

    public static DataTemplate CreateDataTemplate()
    {
        var block = new FrameworkElementFactory(typeof(CheckBox));
        block.SetBinding(CheckBox.CommandProperty, new Binding(""));
        DataTemplate newDataTemplate = new DataTemplate() { VisualTree = block };

    }
1
  • 1
    Have you tried anything yourself yet? Is there a particular part you're having trouble with? Commented Sep 5, 2013 at 8:46

1 Answer 1

3

Try this:

TypeOfYourObject vmLocator = (TypeOfYourObject)Resources["VMLocator"];
CheckBox checkBox = new CheckBox();
checkBox.Command = vmLocator.TimeTableInformationViewModel.MyCommand;
checkBox.CommandParameter = vmLocator.valueFromInput;

UPDATE >>>

There are a number of ways of doing this, but I have included a simple example that includes setting up a Binding... for more than this, please refer to the How do I build a DataTemplate in c# code? post to find out how to create a larger DataTemplate in code.

FrameworkElementFactory checkbox = new FrameworkElementFactory(typeof(CheckBox));
checkBox.Name = "aCheckBox";
checkBox.SetBinding(TextBlock.IsCheckedProperty, new Binding("YourBoolProperty"));
DataTemplate dataTemplate = new DataTemplate() { VisualTree = checkbox };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.