将字符串从 C# 传递到 XAML 按钮

发布于 2024-12-10 12:45:47 字数 997 浏览 0 评论 0原文

这是一个非常简单的问题:我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陪你到最终 2024-12-17 12:45:47

我怀疑您想要类似的东西:

<Button Name="Start" Content="{Binding BtnName}" />

但是您可能还需要为包含的表单/页面/其他内容设置数据上下文。

I suspect you want something like:

<Button Name="Start" Content="{Binding BtnName}" />

but you may then also need to set the data context for the containing form/page/whatever.

作死小能手 2024-12-17 12:45:47

@Jon Skeet 和 @Steve Greatrex 的答案都是有效的,但我想向 @Pimenta 解释它们。

WPF 中的数据绑定与相关。您可以

  1. 在绑定表达式上显式设置此源,就像 @Steve 的回答中的情况一样(在他的例子中,它是一个相对源);

  2. 或者它可以是隐式的,如@Jon的解决方案,在这种情况下,默认源是控件的DataContext,可以手动设置或从其父级继承。

因此,为了让 @Steve 的解决方案发挥作用,您所说的“C# 代码”应该是您的 xaml 背后的代码,他假设它是一个窗口(WPF 中的默认项)。

为了使@Jon 的解决方案发挥作用,您需要为按钮设置DataContext。直接通过编写:

Start.DataContext=this;

或通过将其设置(在代码中)为 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

  1. explicitly set this source on the Binding expression, as was the case in @Steve's answer (in his case it was a relative source);

  2. 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:

Start.DataContext=this;

or by setting it (in code) for a control somewhere up in the xaml hierarchy.

I hope this helps :)

惜醉颜 2024-12-17 12:45:47

为了使绑定正常工作,您需要将字段转换为如下所示的属性。

using System;
using System.Windows;
using System.Windows.Controls;

namespace BarScore
{
    public partial class MainWindow : Window
    {
        private string _BUTTON_NAME_STR = "START";
        public string BUTTON_NAME_STR
        {
           get
           {
                return _BUTTON_NAME_STR;
           }
           set
           {
                _BUTTON_NAME_STR = value;
           }
        }

        public MainWindow()
        {
            InitializeComponent();
            Start.DataContext=this;
        }
    }
}

然后将绑定设置到您的属性

<StackPanel>
    <Button Name="Start" Content="{Binding BUTTON_NAME_STR}"
       Height="25" Width="100"
       Margin="20" />
</StackPanel>

上还有更多链接:

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.

using System;
using System.Windows;
using System.Windows.Controls;

namespace BarScore
{
    public partial class MainWindow : Window
    {
        private string _BUTTON_NAME_STR = "START";
        public string BUTTON_NAME_STR
        {
           get
           {
                return _BUTTON_NAME_STR;
           }
           set
           {
                _BUTTON_NAME_STR = value;
           }
        }

        public MainWindow()
        {
            InitializeComponent();
            Start.DataContext=this;
        }
    }
}

Then set your binding to your property

<StackPanel>
    <Button Name="Start" Content="{Binding BUTTON_NAME_STR}"
       Height="25" Width="100"
       Margin="20" />
</StackPanel>

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

忱杏 2024-12-17 12:45:47

假设您的 BtnName 属性位于父 Window 上,您可以使用以下内容:

<Button Name="Start" Content="{Binding BtnName,
    RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />

Assuming your BtnName property is on the parent Window you can use the following:

<Button Name="Start" Content="{Binding BtnName,
    RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文