开发者

WPF How to set checkbox IsChecked binding in code behind

开发者 https://www.devze.com 2023-02-10 02:25 出处:网络
I have a style that I have to create in code-behind. It has a checkbox that looks like this.. <CheckBox

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消