本文整理汇总了C#中System.Windows.Forms.Form.SetDataContext方法的典型用法代码示例。如果您正苦于以下问题:C# Form.SetDataContext方法的具体用法?C# Form.SetDataContext怎么用?C# Form.SetDataContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.Form
的用法示例。
在下文中一共展示了Form.SetDataContext方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InheritDataContextFromParent
public void InheritDataContextFromParent()
{
using(var form = new Form()) {
var button1 = new Button();
var button2 = new Button();
var button3 = new Button();
button3.SetDataContext("button3");
form.Controls.Add(button1);
form.SetDataContext("test");
form.Controls.Add(button2);
form.Controls.Add(button3);
Assert.That(button1.GetDataContext(), Is.EqualTo("test"));
Assert.That(button2.GetDataContext(), Is.EqualTo("test"));
Assert.That(button3.GetDataContext(), Is.EqualTo("button3"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.HasLocalDataContext(), Is.False);
Assert.That(button3.HasLocalDataContext(), Is.True);
button1.SetDataContext("button1");
Assert.That(button1.GetDataContext(), Is.EqualTo("button1"));
button2.SetDataContext(null);
Assert.That(button2.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.True);
Assert.That(button2.HasLocalDataContext(), Is.True);
button3.ClearDataContext();
Assert.That(button3.GetDataContext(), Is.EqualTo("test"));
Assert.That(button3.HasLocalDataContext(), Is.False);
}
}
示例2: ChangeInheritedDataContext
public void ChangeInheritedDataContext()
{
using(var form = new Form()) {
var button1 = new Button();
var button2 = new Button();
form.Controls.Add(button1);
form.Controls.Add(button2);
form.SetDataContext("test");
Assert.That(button1.GetDataContext(), Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
form.SetDataContext("test2");
Assert.That(button1.GetDataContext(), Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
button2.SetDataContext("button2");
form.ClearDataContext();
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.SetDataContext("test");
Assert.That(button1.GetDataContext(), Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.Controls.Remove(button1);
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.Controls.Remove(button2);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
}
}
示例3: MultiLevelDataContextInheritance2
public void MultiLevelDataContextInheritance2()
{
using(var form = new Form()) {
var panel = new Panel();
var button = new Button();
panel.Controls.Add(button);
form.SetDataContext("test");
form.Controls.Add(panel);
Assert.That(button.GetDataContext(), Is.EqualTo("test"));
Assert.That(button.HasLocalDataContext(), Is.False);
form.SetDataContext("test2");
Assert.That(button.GetDataContext(), Is.EqualTo("test2"));
Assert.That(button.HasLocalDataContext(), Is.False);
panel.SetDataContext("panel");
Assert.That(button.GetDataContext(), Is.EqualTo("panel"));
Assert.That(button.HasLocalDataContext(), Is.False);
}
}
示例4: MultiLevelDataContextInheritance1
public void MultiLevelDataContextInheritance1()
{
using(var form = new Form()) {
var panel = new Panel();
var button1 = new Button();
var button2 = new Button();
panel.Controls.Add(button1);
panel.Controls.Add(button2);
form.Controls.Add(panel);
form.SetDataContext("test");
Assert.That(button1.GetDataContext(), Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
form.SetDataContext("test2");
Assert.That(button1.GetDataContext(), Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
button2.SetDataContext("button2");
form.ClearDataContext();
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.SetDataContext("test2");
Assert.That(button1.GetDataContext(), Is.EqualTo("test2"));
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
Assert.That(button1.HasLocalDataContext(), Is.False);
panel.SetDataContext("panel");
Assert.That(button1.GetDataContext(), Is.EqualTo("panel"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.ClearDataContext();
Assert.That(panel.GetDataContext(), Is.EqualTo("panel"));
Assert.That(panel.HasLocalDataContext(), Is.True);
Assert.That(button1.GetDataContext(), Is.EqualTo("panel"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.SetDataContext("test2");
Assert.That(panel.GetDataContext(), Is.EqualTo("panel"));
Assert.That(panel.HasLocalDataContext(), Is.True);
Assert.That(button1.GetDataContext(), Is.EqualTo("panel"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
panel.ClearDataContext();
Assert.That(panel.GetDataContext(), Is.EqualTo("test2"));
Assert.That(panel.HasLocalDataContext(), Is.False);
Assert.That(button1.GetDataContext(), Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
form.Controls.Clear();
Assert.That(panel.GetDataContext(), Is.Null);
Assert.That(panel.HasLocalDataContext(), Is.False);
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
panel.SetDataContext("panel");
Assert.That(button1.GetDataContext(), Is.EqualTo("panel"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
panel.Controls.Clear();
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.GetDataContext(), Is.EqualTo("button2"));
Assert.That(button2.HasLocalDataContext(), Is.True);
}
}
示例5: UndoSetBindingAndValue
public void UndoSetBindingAndValue()
{
using(var form = new Form()) {
//form initialized
BindingManager manager = new BindingManager();
((ISupportInitialize)manager).BeginInit();
((ISupportInitialize)manager).EndInit();
//binding created with wizard
manager.SetBinding(form, TextProperty, new Binding());
//value set with property grid
manager.SetValue(form, TextProperty, "test");
//deserialization on undo
((ISupportInitialize)manager).BeginInit();
Assert.That(form.GetValue(TextProperty), Is.Null);
manager.SetBinding(form, TextProperty, new Binding());
((ISupportInitialize)manager).EndInit();
//undo from property grid
manager.ClearValue(form, TextProperty);
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
Assert.That(manager.GetValueActions().Count(), Is.EqualTo(0));
form.SetDataContext("Test");
Assert.That(form.GetValue(TextProperty), Is.EqualTo("Test"));
}
}
示例6: SetClearBindings
public void SetClearBindings()
{
using(var form = new Form()) {
var button = new Button();
form.Controls.Add(button);
var viewModel = new TestViewModel() { StringProperty = "test", StringProperty2 = "test2" };
form.SetDataContext(viewModel);
BindingManager manager = new BindingManager();
manager.SetBinding(form, "Text", new Binding("StringProperty"));
Assert.That(form.Text, Is.EqualTo("test"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
Assert.That(manager.GetValueActions().Count(), Is.EqualTo(0));
SetBindingAction action = manager.GetBindingActions().First();
Assert.That(action.Control, Is.EqualTo(form));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
Assert.That(action.Property, Is.InstanceOf<StandardPropertyDescriptor>());
Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty"));
manager.SetBinding(button, "Text", new Binding("StringProperty2"));
Assert.That(button.Text, Is.EqualTo("test2"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));
action = manager.GetBindingActions().ElementAt(1);
Assert.That(action.Control, Is.EqualTo(button));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
manager.SetBinding(form, "Text", new Binding("StringProperty2"));
Assert.That(form.Text, Is.EqualTo("test2"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));
action = manager.GetBindingActions().First();
Assert.That(action.Control, Is.EqualTo(form));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty2"));
manager.ClearBinding(form, "Text");
Assert.That(form.Text, Is.EqualTo(string.Empty));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
action = manager.GetBindingActions().First();
Assert.That(action.Control, Is.EqualTo(button));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
manager.SetBinding(form, "Text", new Binding("StringProperty"));
manager.SetBinding(form, "Tag", new Binding("StringProperty"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(3));
manager.ClearAllBindings(form);
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
action = manager.GetBindingActions().First();
Assert.That(action.Control, Is.EqualTo(button));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
manager.SetBinding(form, TextProperty, new Binding("StringProperty"));
Assert.That(form.GetValue(TextProperty), Is.EqualTo("test"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));
action = manager.GetBindingActions().ElementAt(1);
Assert.That(action.Control, Is.EqualTo(form));
Assert.That(action.Property.Name, Is.EqualTo("Text"));
Assert.That(action.Property, Is.InstanceOf<AttachedPropertyDescriptor>());
Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty"));
manager.ClearBinding(form, TextProperty);
Assert.That(form.GetValue(TextProperty), Is.EqualTo(null));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
Assert.That(manager.GetBindingActions().First().Property, Is.InstanceOf<StandardPropertyDescriptor>());
manager.SetBinding(button, TextProperty, new Binding("StringProperty2"));
Assert.That(button.GetValue(TextProperty), Is.EqualTo("test2"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));
manager.ClearAllBindings(button);
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
}
}
示例7: SetClearValue
public void SetClearValue()
{
using(var form = new Form()) {
var button = new Button();
form.Controls.Add(button);
var viewModel = new TestViewModel() { StringProperty = "test", StringProperty2 = "test2" };
form.SetDataContext(viewModel);
BindingManager manager = new BindingManager();
manager.SetBinding(button, DataContextProvider.DataContextProperty, new Binding(() => viewModel.StringProperty));
manager.SetValue(button, DataContextProvider.DataContextProperty, "value");
Assert.That(button.GetDataContext(), Is.EqualTo("value"));
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
Assert.That(manager.GetValueActions().Count(), Is.EqualTo(1));
SetValueAction action = manager.GetValueActions().First();
Assert.That(action.Control, Is.EqualTo(button));
Assert.That(((AttachedPropertyDescriptor)action.Property).Property, Is.EqualTo(DataContextProvider.DataContextProperty));
Assert.That(action.Value, Is.EqualTo("value"));
Assert.That(manager.FindAction(button, AttachedPropertyDescriptor.FromAttachedProperty(DataContextProvider.DataContextProperty)), Is.EqualTo(action));
manager.SetValue(button, DataContextProvider.DataContextProperty, "value2");
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
Assert.That(manager.GetValueActions().Count(), Is.EqualTo(1));
Assert.That(button.GetDataContext(), Is.EqualTo("value2"));
action = manager.GetValueActions().First();
Assert.That(action.Control, Is.EqualTo(button));
Assert.That(((AttachedPropertyDescriptor)action.Property).Property, Is.EqualTo(DataContextProvider.DataContextProperty));
Assert.That(action.Value, Is.EqualTo("value2"));
manager.ClearValue(button, DataContextProvider.DataContextProperty);
Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
Assert.That(manager.GetValueActions().Count(), Is.EqualTo(0));
Assert.That(button.GetDataContext(), Is.Null);
}
}
示例8: MultiLevelDataContextInheritance1
public void MultiLevelDataContextInheritance1()
{
using(var form = new Form()) {
var panel = new Panel();
var button1 = new Button();
button1.SetBinding("Text", new Binding());
panel.SetBinding("Tag", new Binding());
panel.Controls.Add(button1);
form.Controls.Add(panel);
form.SetDataContext("test");
Assert.That(button1.Text, Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
form.SetDataContext("test2");
Assert.That(button1.Text, Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
form.ClearDataContext();
Assert.That(button1.Text, Is.EqualTo(string.Empty));
form.SetDataContext("test2");
Assert.That(panel.Tag, Is.EqualTo("test2"));
Assert.That(button1.Text, Is.EqualTo("test2"));
panel.SetDataContext("panel");
Assert.That(panel.Tag, Is.EqualTo("panel"));
Assert.That(button1.Text, Is.EqualTo("panel"));
form.ClearDataContext();
Assert.That(panel.Tag, Is.EqualTo("panel"));
Assert.That(button1.Text, Is.EqualTo("panel"));
form.SetDataContext("test2");
Assert.That(panel.Tag, Is.EqualTo("panel"));
Assert.That(button1.Text, Is.EqualTo("panel"));
panel.ClearDataContext();
Assert.That(panel.Tag, Is.EqualTo("test2"));
Assert.That(button1.Text, Is.EqualTo("test2"));
form.Controls.Clear();
Assert.That(panel.Tag, Is.Null);
Assert.That(button1.Text, Is.EqualTo(string.Empty));
}
}
示例9: InheritedDataContextBinding_BindBeforeAddToTree
public void InheritedDataContextBinding_BindBeforeAddToTree()
{
using(var form = new Form()) {
var button1 = new Button();
var button2 = new Button();
button1.SetBinding("Text", new Binding());
button2.SetBinding("Text", new Binding());
form.Controls.Add(button1);
form.SetDataContext("test");
form.Controls.Add(button2);
Assert.That(button1.Text, Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.Text, Is.EqualTo("test"));
Assert.That(button2.HasLocalDataContext(), Is.False);
}
}
示例10: InheritedDataContextBinding_BindAfterAddToTree
public void InheritedDataContextBinding_BindAfterAddToTree()
{
using(var form = new Form()) {
var button1 = new Button();
var button2 = new Button();
form.Controls.Add(button1);
form.SetDataContext("test");
form.Controls.Add(button2);
button1.SetBinding("Text", new Binding());
Assert.That(button1.Text, Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.False);
button2.SetBinding("Text", new Binding());
Assert.That(button2.Text, Is.EqualTo("test"));
Assert.That(button2.HasLocalDataContext(), Is.False);
form.SetDataContext("test2");
Assert.That(button1.Text, Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.Text, Is.EqualTo("test2"));
Assert.That(button2.HasLocalDataContext(), Is.False);
button1.SetDataContext("button1");
Assert.That(button1.Text, Is.EqualTo("button1"));
Assert.That(button1.HasLocalDataContext(), Is.True);
button2.SetDataContext(null);
Assert.That(button2.Text, Is.EqualTo(string.Empty));
Assert.That(button2.HasLocalDataContext(), Is.True);
button1.ClearDataContext();
Assert.That(button1.Text, Is.EqualTo("test2"));
Assert.That(button1.HasLocalDataContext(), Is.False);
button2.ClearDataContext();
Assert.That(button2.Text, Is.EqualTo("test2"));
Assert.That(button2.HasLocalDataContext(), Is.False);
form.ClearDataContext();
Assert.That(button1.Text, Is.EqualTo(string.Empty));
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(button2.Text, Is.EqualTo(string.Empty));
Assert.That(button2.HasLocalDataContext(), Is.False);
button1.SetDataContext("button1");
Assert.That(button1.Text, Is.EqualTo("button1"));
Assert.That(button1.HasLocalDataContext(), Is.True);
form.SetDataContext("test");
Assert.That(button1.Text, Is.EqualTo("button1"));
Assert.That(button1.HasLocalDataContext(), Is.True);
Assert.That(button2.Text, Is.EqualTo("test"));
Assert.That(button2.HasLocalDataContext(), Is.False);
form.Controls.Clear();
Assert.That(button1.Text, Is.EqualTo("button1"));
Assert.That(button2.Text, Is.EqualTo(string.Empty));
}
}
示例11: BindingToPathWithInheritedDataContext
public void BindingToPathWithInheritedDataContext()
{
var viewModel = new TestViewModel();
using(var form = new Form()) {
var button1 = new Button() { Text = "button1" };
form.Controls.Add(button1);
form.SetDataContext(viewModel);
button1.SetBinding(() => button1.Text, new Binding("StringProperty"));
Assert.That(button1.Text, Is.EqualTo(string.Empty));
viewModel.StringProperty = "test";
Assert.That(button1.Text, Is.EqualTo("test"));
}
}