本文整理汇总了C#中System.Windows.Forms.Button.SetDataContext方法的典型用法代码示例。如果您正苦于以下问题:C# Button.SetDataContext方法的具体用法?C# Button.SetDataContext怎么用?C# Button.SetDataContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.Button
的用法示例。
在下文中一共展示了Button.SetDataContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: ClearDataContextWhenThereIsNoInheritedValueFromParent
public void ClearDataContextWhenThereIsNoInheritedValueFromParent()
{
using(var form = new Form()) {
var button1 = new Button();
button1.SetDataContext("button3");
form.Controls.Add(button1);
button1.ClearDataContext();
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
Assert.That(form.HasLocalDataContext(), Is.False);
}
}
示例3: 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);
}
}
示例4: BindAttachedProperty2
public void BindAttachedProperty2()
{
var viewModel = new TestViewModel();
using(var button = new Button()) {
button.SetValue(TextProperty, "button1");
button.SetDataContext(viewModel);
button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().StringProperty2));
button.SetBinding(TextProperty, new Binding(() => new TestViewModel().StringProperty));
Assert.That(button.GetValue(TextProperty), Is.EqualTo(null));
Assert.That(button.Text, Is.EqualTo(string.Empty));
viewModel.StringProperty = "test";
Assert.That(button.GetValue(TextProperty), Is.EqualTo("test"));
Assert.That(button.Text, Is.EqualTo(string.Empty));
viewModel.StringProperty2 = "test2";
Assert.That(button.GetValue(TextProperty), Is.EqualTo("test"));
Assert.That(button.Text, Is.EqualTo("test2"));
button.ClearBinding(TextProperty);
Assert.That(button.GetValue(TextProperty), Is.Null);
}
}
示例5: DoCollectControlWithDataContextSet
WeakReference DoCollectControlWithDataContextSet()
{
var button = new Button();
button.SetDataContext("test");
return new WeakReference(button);
}
示例6: SetAndGetDataContextForControl
public void SetAndGetDataContextForControl()
{
using(var button1 = new Button()) {
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
button1.SetDataContext("test");
Assert.That(button1.GetDataContext(), Is.EqualTo("test"));
Assert.That(button1.HasLocalDataContext(), Is.True);
button1.ClearDataContext();
Assert.That(button1.GetDataContext(), Is.Null);
Assert.That(button1.HasLocalDataContext(), Is.False);
}
using(var button2 = new Button()) {
Assert.That(button2.GetDataContext(), Is.Null);
button2.SetDataContext(13);
Assert.That(button2.GetDataContext(), Is.EqualTo(13));
}
using(var button3 = new Button()) {
button3.ClearDataContext();
Assert.That(button3.GetDataContext(), Is.Null);
Assert.That(button3.HasLocalDataContext(), Is.False);
}
}
示例7: 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);
}
}
示例8: TwoWayBindingForAttachedProperty
public void TwoWayBindingForAttachedProperty()
{
var viewModel = new TestViewModel() { };
using(var button = new Button()) {
button.SetDataContext(viewModel);
PropertyEntry<object> entry = CommandProvider.CommandParameterProperty.GetPropertyEntry(button);
Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(0));
button.SetBinding(CommandProvider.CommandParameterProperty, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(1));
Assert.That(button.GetCommandParameter(), Is.Null);
button.SetCommandParameter("test");
Assert.That(viewModel.StringProperty, Is.EqualTo("test"));
button.ClearBinding(CommandProvider.CommandParameterProperty);
Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(0));
Assert.That(viewModel.StringProperty, Is.EqualTo("test"));
Assert.That(button.GetCommandParameter(), Is.Null);
viewModel.StringProperty = "test2";
Assert.That(button.GetCommandParameter(), Is.Null);
button.SetCommandParameter("test3");
}
}
示例9: DoBindButtonAndGetViewModelReference
WeakReference DoBindButtonAndGetViewModelReference(Button button)
{
var viewModel = new TestViewModel() { StringProperty = "test" };
button.SetDataContext(viewModel);
button.SetBinding("Text", new Binding(() => viewModel.StringProperty));
return new WeakReference(viewModel);
}
示例10: SetSimpleBindingAfterSetDataContext
public void SetSimpleBindingAfterSetDataContext()
{
using(var button = new Button()) {
button.SetDataContext("test");
button.SetBinding("Text", new Binding());
Assert.That(button.Text, Is.EqualTo("test"));
button.SetDataContext("test2");
Assert.That(button.Text, Is.EqualTo("test2"));
int textChangedCount = 0;
button.TextChanged += (sender, e) => {
textChangedCount++;
};
button.SetDataContext("test2");
Assert.That(textChangedCount, Is.EqualTo(0));
button.ClearDataContext();
Assert.That(button.Text, Is.EqualTo(string.Empty));
button.SetDataContext("test3");
Assert.That(button.Text, Is.EqualTo("test3"));
}
}
示例11: SetSimpleBindingBeforeSetDataContext
public void SetSimpleBindingBeforeSetDataContext()
{
using(var button = new Button()) {
button.SetBinding("Text", new Binding());
Assert.That(button.HasLocalDataContext(), Is.False);
button.SetDataContext("test");
Assert.That(button.Text, Is.EqualTo("test"));
Assert.That(button.HasLocalDataContext(), Is.True);
button.SetDataContext("test2");
Assert.That(button.Text, Is.EqualTo("test2"));
button.ClearDataContext();
Assert.That(button.Text, Is.EqualTo(string.Empty));
button.SetDataContext("test3");
Assert.That(button.Text, Is.EqualTo("test3"));
button.ClearBinding("Text");
button.ClearBinding("Tag");
Assert.That(button.Text, Is.EqualTo(string.Empty));
Assert.That(button.Tag, Is.Null);
button.SetDataContext("test4");
Assert.That(button.Text, Is.EqualTo(string.Empty));
}
}
示例12: SetBindingWithLambdaExpression
public void SetBindingWithLambdaExpression()
{
using(var button = new Button()) {
button.SetBinding(() => button.Text, new Binding());
button.SetDataContext("test");
Assert.That(button.Text, Is.EqualTo("test"));
button.ClearBinding(() => button.Text);
Assert.That(button.Text, Is.EqualTo(string.Empty));
}
}
示例13: 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));
}
}
示例14: ExtractPathFromLambdaExpression
public void ExtractPathFromLambdaExpression()
{
var viewModel = new TestViewModel() { StringProperty = "StringProperty", NestedViewModel = new NestedTestViewModel() { NestedStringProperty = "NestedStringProperty" } };
using(var button = new Button()) {
button.SetDataContext(viewModel);
button.SetBinding(() => button.Text, new Binding(() => viewModel.StringProperty));
Assert.That(button.Text, Is.EqualTo("StringProperty"));
button.SetBinding(() => button.Text, new Binding(() => viewModel.NestedViewModel.NestedStringProperty));
Assert.That(button.Text, Is.EqualTo("NestedStringProperty"));
button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().StringProperty));
Assert.That(button.Text, Is.EqualTo("StringProperty"));
button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().NestedViewModel.NestedStringProperty));
Assert.That(button.Text, Is.EqualTo("NestedStringProperty"));
}
}
示例15: ConvertToTargetPropertyType
public void ConvertToTargetPropertyType()
{
var viewModel = new TestViewModel() { IntProperty = 9, DoubleProperty = 153.2 };
using(var button = new Button()) {
button.SetDataContext(viewModel);
button.SetBinding(() => button.Text, new Binding(() => viewModel.IntProperty));
Assert.That(button.Text, Is.EqualTo("9"));
viewModel.IntProperty = 13;
Assert.That(button.Text, Is.EqualTo("13"));
button.SetBinding(() => button.TabIndex, new Binding(() => viewModel.DoubleProperty));
Assert.That(button.TabIndex, Is.EqualTo(153));
}
}