[英]WPF dynamic binding of a control property to another control property
I have a question - I was coding happily a quick little feature to an app, which was a simple comparison output window. 我有一个问题-我很高兴为应用程序编写了一个快速的小功能,这是一个简单的比较输出窗口。 Basically, user clicks a button and I generate a window with a datagrid of two columns of data.
基本上,用户单击一个按钮,然后生成一个包含两列数据的数据网格的窗口。
It's all great and a five minutes code living inside one method with no unnecessary reference to anything else. 一切都很好,一个方法中包含了五分钟的代码,没有不必要的引用。 The only problem I encountered was when I wanted to add a TopMost checkbox to this window.
我遇到的唯一问题是当我想向该窗口添加TopMost复选框时。 How do I bind the IsChecked property of the box to the TopMost property of the window?
如何将框的IsChecked属性绑定到窗口的TopMost属性?
var checkbox = new CheckBox();
checkbox.Name = "cb";
checkbox.Content = "Top most";
var grid = new DataGrid();
grid.ItemsSource = wcList;
grid.Margin = new Thickness(5);
var panel = new StackPanel();
panel.Children.Add(checkbox);
panel.Children.Add(grid);
var win = new Window();
//Binding b = new Binding("cb");
//b.Mode = BindingMode.TwoWay;
//b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//win.SetBinding(Window.TopmostProperty, b);
win.Title = "WordCount comparison";
win.Content = panel;
win.SizeToContent = SizeToContent.WidthAndHeight;
win.Icon = this.Icon;
win.Show();
This was supposed to be a 5-minutes one-off method, which is why I don't want to go as far as adding any xaml or properties into the code. 这本应是一种5分钟的一次性方法,这就是为什么我不想在代码中添加任何xaml或属性的原因。
Cheers Bartek 欢呼声Bartek
The other way around as you tried (in the commented code): 尝试的另一种方法(在注释的代码中):
checkbox.SetBinding(CheckBox.IsCheckedProperty, new Binding("Topmost") { Source = win });
just after you instantiated your win variable. 在实例化您的win变量之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.