将字符串从 C# 传递到 XAML 按钮
这是一个非常简单的问题:我在 C# 类中定义了一个字符串,我希望它是 XAML 按钮的内容。就是这样。我可以在里面放什么????我知道这是基本的 WPF...我将继续寻找答案。提前致谢。
我的 XAML
<Window x:Class="BarScore.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<StackPanel>
<Button Name="Start" Content="???" Height="25" Width="100"
Margin="20" />
</StackPanel>
</Window>
我的 C# 代码
using System;
using System.Windows;
using System.Windows.Controls;
namespace BarScore
{
public partial class MainWindow : Window
{
public string BUTTON_NAME_STR = "START";
public MainWindow()
{
InitializeComponent();
}
}
}
PS:我知道我可以转到代码并编写:
Start.Content = BUTTON_NAME_STR;
但如果可能的话,我希望它是从 XAML 到 C#,而不是相反。
This is a very simple question: I have a string defined in my C# class and I would like it to be the content of a XAML button. That's it. What can I put in the ????. I know this is basic WPF... I will keep looking for an answer. Thanks in advance.
My XAML
<Window x:Class="BarScore.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<StackPanel>
<Button Name="Start" Content="???" Height="25" Width="100"
Margin="20" />
</StackPanel>
</Window>
My C# code
using System;
using System.Windows;
using System.Windows.Controls;
namespace BarScore
{
public partial class MainWindow : Window
{
public string BUTTON_NAME_STR = "START";
public MainWindow()
{
InitializeComponent();
}
}
}
PS: I know I can go to the code and write:
Start.Content = BUTTON_NAME_STR;
but I would like it to be from XAML to C# and not the other way around, if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑您想要类似的东西:
但是您可能还需要为包含的表单/页面/其他内容设置数据上下文。
I suspect you want something like:
but you may then also need to set the data context for the containing form/page/whatever.
@Jon Skeet 和 @Steve Greatrex 的答案都是有效的,但我想向 @Pimenta 解释它们。
WPF 中的数据绑定与源相关。您可以
在绑定表达式上显式设置此源,就像 @Steve 的回答中的情况一样(在他的例子中,它是一个相对源);
或者它可以是隐式的,如@Jon的解决方案,在这种情况下,默认源是控件的
DataContext
,可以手动设置或从其父级继承。因此,为了让 @Steve 的解决方案发挥作用,您所说的“C# 代码”应该是您的 xaml 背后的代码,他假设它是一个窗口(WPF 中的默认项)。
为了使@Jon 的解决方案发挥作用,您需要为按钮设置
DataContext
。直接通过编写:或通过将其设置(在代码中)为 xaml 层次结构中某处的控件。
我希望这有帮助:)
Both @Jon Skeet and @Steve Greatrex's answers are valid, but I would like to explain them to @Pimenta.
Databinding in WPF is relative to a Source. You can
explicitly set this source on the Binding expression, as was the case in @Steve's answer (in his case it was a relative source);
or it can be implicit, as in @Jon's solution, in which case, the default source is the
DataContext
of the control, which could be set manually or inherited from its parent.So, for @Steve's solution to work, what you refer to as your "C# code" should be the code behind for your xaml, which he assumes to be a window (the default item in WPF).
For @Jon's solution to work, you need to set the
DataContext
for your button. Either directly by writing:or by setting it (in code) for a control somewhere up in the xaml hierarchy.
I hope this helps :)
为了使绑定正常工作,您需要将字段转换为如下所示的属性。
然后将绑定设置到您的属性
上还有更多链接:
http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx
For the binding to work, you will need to turn your field into a property like the following.
Then set your binding to your property
Some more links on it :
http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx
http://msdn.microsoft.com/en-us/magazine/cc163299.aspx
假设您的
BtnName
属性位于父Window
上,您可以使用以下内容:Assuming your
BtnName
property is on the parentWindow
you can use the following: