-2

「クリックしてください!」と表示されるボタンがあります。その上で。クリックした後に見たいのですが、「私をクリックしないでください!」と表示されます。永久に。

関数で以下のエラー メッセージが表示されますが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!";
        }   
    }
}
4

2 に答える 2

0

代わりにこれを試すことができます:

    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>
于 2014-01-31T19:29:00.080 に答える