Reputation: 12843
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 };
}
Upvotes: 1
Views: 3055
Reputation: 69959
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 };
Upvotes: 3