Binding Control IsChecked在后面的代码中

Binding Control IsChecked in the code behind

我正在动态生成我的用户界面的一些元素,以防止在我的 windows 中重复相同的代码,因为它们在设计上非常相似。我正在生成的一个控件是一个切换开关,目前在 xaml:

中像这样绑定
IsChecked="{Binding DebugEnabled, Source={StaticResource NotifyFields}}

当生成此控件时,如何将其移动到后面的 C# 代码中?

var tsGeneratedSwitch = new ToggleSwitch
{
    Header = "View Information",
    OffLabel = "No",
    OnLabel = "Yes",
    IsChecked = False,
};
tsGeneratedSwitch.Checked += SwitchChanged;
tsGeneratedSwitch.Unchecked += SwitchChanged;

非常感谢

var myBinding = new Binding
{
    Source = NotifyFields,
    Path = new PropertyPath("DebugEnabled"),
    Mode = BindingMode.TwoWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};

BindingOperations.SetBinding(tsGeneratedSwitch, CheckBox.IsCheckedProperty, myBinding);