【问题标题】:Changing the text of the button (at runtime) [duplicate]更改按钮的文本(在运行时)[重复]
【发布时间】:2014-01-31 19:26:39
【问题描述】:

我有一个显示“点击我!”的按钮在上面。我想在点击后看到,它显示“不要点击我!”永久。

我在buttonname_click() 函数中收到以下错误消息,我不知道如何解决它(我花了这么多时间)。

名称“buttonname”在当前上下文中不存在

我在这个问题中附上了 Search.xaml 和 Search.xaml.cs。

<sdk:Page   
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"   
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:local="clr-namespace:TMTemplate"
xmlns:mocal="Clr-namespace:DynamicNavigation"
DataContext="{Binding RelativeSource={RelativeSource self}}"
xmlns:ContactForm="clr-namespace:ContactForm"
mc:Ignorable="d"
x:Class="TMTemplate.Search" 
Title="Search"
d:DesignWidth="640" d:DesignHeight="480" Height="530" Width="700">
<Grid x:Name="LayoutRoot" >
    <StackPanel Margin="50,45,25,45">
        <Button Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>
    </StackPanel>
</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace TMTemplate
{
    public partial class Search : Page
    {
        public Search() { 
         InitializeComponent();
        }
        private void buttonname_Click(object sender, RoutedEventArgs e)
        {
         buttonname.Content = "Do not click on me!";
        }   
    }
}

【问题讨论】:

  • 我是昨天问这个的人,但仍然无法继续
  • 不要转发相同的问题。如果您有要添加的信息,请编辑原件。
  • 这个问题似乎离题了,因为它没有显示出对编程的基本理解。
  • Hi lan Ringrose,你对编程有专业的理解,这很好但是我想让你知道我正在攻读硕士学位,过去 9 年我一直在编程。我是 Silverlight 的新手,遇到了这个问题。我不知道这是否是一个错误,但它不起作用,这就是它......非常感谢你

标签: c# wpf silverlight xaml dynamic-text


【解决方案1】:

你可以试试这个:

    private void buttonname_Click(object sender, RoutedEventArgs e)
    {
     ((Button)sender).Content = "Do not click on me!";
    } 

但是,要按原样运行代码,您需要使用 x:Name 而不是 Name 指定名称:

<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>

【讨论】:

  • 谢谢,但是它说“不能隐式地将类型'string'转换为'System.Windows.UIElement'
  • 这很奇怪。这两种方法都应该有效。
【解决方案2】:
<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>

private void buttonname_Click(object sender, RoutedEventArgs e)
    {
     this.buttonname.Content = "Do not click on me!";
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多