I have a style that I have to create in code-behind. It has a checkbox that looks like this..
<CheckBox
开发者_C百科 HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=DataItem.IsChecked}"
>
</CheckBox>
How do I replicate this in code-behind?
Something like this:
CheckBox myCheckBox = new CheckBox();
myCheckBox.HorizontalAlignment = HorizontalAlignment.Center;
myCheckBox.VerticalAlignment = VerticalAlignment.Center;
myCheckBox.SetBinding(ToggleButton.IsCheckedProperty, "DataItem.IsChecked");
var myCheckBox = new CheckBox() {DataContext = DataItem };
myCheckBox.SetBinding(ToggleButton.IsCheckedProperty, new Binding(nameof(DataItem.IsChecked));
精彩评论