简体   繁体   English

如何将按钮属性从XAML传递到.CS文件

[英]How To Pass Button Properties from XAML to .CS File

I am new to c#. 我是C#的新手。 i want to pass button properties from xaml file to .cs file. 我想将按钮属性从xaml文件传递到.cs文件。 How can i do it? 我该怎么做?

<Button Content="myButton" x:Name="{Binding assignment_title}" click="btn_Click()"/>

Here I want to pass 'Name' of button to .cs file. 在这里,我想将按钮的“名称”传递给.cs文件。 Please guide me. 请指导我。

The properties of a control are exposed to your code behind (.cs) automagically. 控件的属性自动显示在(.cs)后面的代码中。 In your case you're binding the Button 's name which is confusing, I feel like you're actually trying to bind the Content property and that you've placed the name in the Content property by mistake. 在您的情况下,您正在绑定Button的名称,这很令人困惑,我感觉您实际上是在试图绑定Content属性,并且您已经错误地将名称放置在Content属性中。 Try this: 尝试这个:

<Button x:Name="myButton" Content="{Binding assignment_title}" click="btn_Click()"/>

and then in your code behind you can do: 然后在后面的代码中可以执行以下操作:

myButton.Content = "Button Clicked!"

and it will update the content of the Button . 它将更新Button的内容。

在.cs文件中,您可以访问控件属性,如controlName.Property(在您的情况下为myButton.Name)。

Oren's answer is correct, 奥伦的答案是正确的

But i would suggest you look into using ViewModels with your xaml Views, and look into the MVVM pattern and using as less code behind as possible. 但是我建议您考虑将ViewModels与xaml Views一起使用,并考虑MVVM模式并使用尽可能少的代码。

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

I use the mvvm with Caliburn.Micro. 我将mvvm与Caliburn.Micro一起使用。

http://caliburnmicro.codeplex.com/ http://caliburnmicro.codeplex.com/

That does all the bindings using conventions. 这样做使用约定进行所有绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM