[英]Changing button content in WPF
So I am doing a little Tic Tac Toe to learn C#.所以我在做一点井字游戏来学习 C#。 Basically I have 9 buttons and if they get clicked they need to change the content.
基本上我有 9 个按钮,如果他们被点击,他们需要更改内容。 But if I try x:Name="namename" I get an error.
但是,如果我尝试 x:Name="namename" 我会收到错误消息。 So how can I change the button content?
那么如何更改按钮内容呢?
XAML-Code: XAML 代码:
<Window Click:Class="FickFackFo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Click="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:local="clr-namespace:FickFackFo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" SizeToContent="WidthAndHeight" ResizeMode="NoResize">
<Grid>
<Rectangle HorizontalAlignment="Left" Height="300" Margin="165,45,0,0" Stroke="Black" VerticalAlignment="Top" Width="300"/>
<Button Click="X1" Content="X" HorizontalAlignment="Left" Margin="165,45,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X2" Content="X" HorizontalAlignment="Left" Margin="265,45,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X3" Content="X" HorizontalAlignment="Left" Margin="365,45,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X4" Content="X" HorizontalAlignment="Left" Margin="165,145,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X5" Content="X" HorizontalAlignment="Left" Margin="265,145,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X6" Content="X" HorizontalAlignment="Left" Margin="365,145,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X7" Content="X" HorizontalAlignment="Left" Margin="165,245,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X8" Content="X" HorizontalAlignment="Left" Margin="265,245,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
<Button Click="X9" Content="X" HorizontalAlignment="Left" Margin="365,245,0,0" VerticalAlignment="Top" Height="100" Width="100" FontSize="50" ClickMode="Press" Background="White"/>
</Grid>
</Window>
Behind-Code:背后代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace FickFackFo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int round = 0;
public MainWindow()
{
InitializeComponent();
}
private void X1(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X2(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X3(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X4(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X5(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X6(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X7(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X8(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
private void X9(object sender, RoutedEventArgs e)
{
if (round == 0 || round == 2 || round == 4 || round == 6 || round == 8)
{
Content = "X";
}
else if (round == 1 || round == 3 || round == 5 || round == 7 || round == 9)
{
Content = "O";
}
round++;
}
}
}
I already said everything but it says I should add details so: This is a detail.我已经说了一切,但它说我应该添加细节:这是一个细节。
You simply need to set the sender
s Content property and you can vastly simplify your code by using only one
Command
Event Handler because it will always receive the button firing the event:您只需要设置
sender
的 Content 属性,并且您可以通过仅使用一个
命令
事件处理程序来大大简化您的代码,因为它总是会收到触发事件的按钮:
Xaml (part) Xaml (部分)
...
<Button x:Name="button1" Click="button_Click" Content="X" ...
<Button x:Name="button2" Click="button_Click" Content="X" ...
<Button x:Name="button3" Click="button_Click" Content="X" ...
...
Code behind背后的代码
private void button_Click(object sender, RoutedEventArgs e)
{
// break after 9 rounds
if (round > 9)
{
// your code doesn't do anything after round 9 so I'll also just stop as well
return;
}
// cast object sender to Button class
Button btn = (Button)sender;
// use modulo to get rid of all those if conditions
if (round % 2 == 0)
{
btn.Content = "X"; // set Button.Content property
}
else
{
btn.Content = "O"; // set Button.Content property
}
round++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.