绑定控件IsChecked后面的代码

时间:2015-08-02 08:28:10

标签: c# binding

我正在动态生成用户界面的一些元素,以防止在我的窗口中重复相同的代码,因为它们在设计上非常相似。我生成的一个控件是一个切换开关,当前在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;

非常感谢

1 个答案:

答案 0 :(得分:1)

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

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