c# - WPF 将标签内容绑定(bind)到变量

标签 c# wpf xaml

我创建了一个简单的 Tic Tac Toe 应用程序来学习 WPF。但是我无法将 o_win_counter 绑定(bind)到我的 o_win_count 标签。如何做到这一点?稍后我还想将其他计数器标签绑定(bind)到变量。

应用.xaml

<Application x:Class="FirstApplication.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
        <ResourceDictionary Source="ResourceDictionary.xaml" />
</Application.Resources>

ResourceDictionary.xaml

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="Button">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Foreground" Value="#FF959595" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="10,0" />
    <Setter Property="Margin" Value="2" />
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="Height" Value="25" />
    <Setter Property="MinWidth" Value="100" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <ColorAnimation To="#FFFFFFFF" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                Duration="0:0:0.07" />
                                    <ColorAnimation To="#FFDEDEDE" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Duration="0:0:0.07" />
                                    <ColorAnimation To="#FF959595" Storyboard.TargetName="BrBrush"
                                                Storyboard.TargetProperty="Color" Duration="0:0:0.07" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation To="#FF00B4E4" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                Duration="0:0:0.07" />
                                    <ColorAnimation To="#FF0083C3" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Duration="0:0:0.07" />
                                    <ColorAnimation To="#FF4C7B8F" Storyboard.TargetName="BrBrush"
                                                Storyboard.TargetProperty="Color" Duration="0:0:0.07" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation To="#DBEDFD" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                Duration="0:0:0.05" />
                                    <ColorAnimation To="#C4E0FC" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Duration="0:0:0.05" />
                                    <ColorAnimation To="#4C7B8F" Storyboard.TargetName="BrBrush"
                                                Storyboard.TargetProperty="Color" Duration="0:0:0.05" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimation To="#EB2828" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[0].(GradientStop.Color)"
                                                Duration="0:0:0" />
                                    <ColorAnimation To="#EB2828" Storyboard.TargetName="BgBrush"
                                                Storyboard.TargetProperty="(GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                                Duration="0:0:0" />
                                    <ColorAnimation To="#D9D9D9" Storyboard.TargetName="BrBrush"
                                                Storyboard.TargetProperty="Color" Duration="0:0:0" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Chrome" BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="true">
                        <Border.BorderBrush>
                            <SolidColorBrush x:Name="BrBrush" Color="#ACACAC" />
                        </Border.BorderBrush>
                        <Border.Background>
                            <LinearGradientBrush x:Name="BgBrush" EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#FFF0F0F0" Offset="0" />
                                <GradientStop Color="#FFE5E5E5" Offset="1" />
                            </LinearGradientBrush>
                        </Border.Background>
                        <ContentPresenter
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="#FFFFFF" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Foreground" Value="#000000" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

主窗口.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 FirstApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public bool turn = true;
public int turn_count = 0;
private int _o_win_counter;

public MainWindow()
{
    InitializeComponent();
}

public int o_win_counter
{
    get
    {
        return _o_win_counter;
    }
    set
    {
        if (_o_win_counter != value)
        {
            _o_win_counter = value;
            OnPropertyChanged("o_win_counter");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

private void button_click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    if (turn)
        button.Content = "X";
    else
    {
        button.Content = "O";
    }

    turn = !turn;
    button.IsEnabled = false;
    turn_count++;

    checkForWinner();
}

private void checkForWinner()
{
    bool isWinner = false;

    //horizontal
    if (A.Content == B.Content && (B.Content == C.Content) && (!A.IsEnabled))
        isWinner = true;
    else if (D.Content == E.Content && (D.Content == F.Content) && (!D.IsEnabled))
        isWinner = true;
    else if (G.Content == H.Content && (G.Content == I.Content) && (!G.IsEnabled))
        isWinner = true;

    //vertical
    else if (A.Content == D.Content && (D.Content == G.Content) && (!A.IsEnabled))
        isWinner = true;
    else if (B.Content == E.Content && (B.Content == H.Content) && (!B.IsEnabled))
        isWinner = true;
    else if (C.Content == F.Content && (C.Content == I.Content) && (!C.IsEnabled))
        isWinner = true;

    //diagonal
    else if (A.Content == E.Content && (E.Content == I.Content) && (!A.IsEnabled))
        isWinner = true;
    else if (G.Content == E.Content && (E.Content == C.Content) && (!G.IsEnabled))
        isWinner = true;


    if (isWinner)
    {
        disableButtons();

        String winner = "";
        if (turn)
        {
            winner = "O";
        }
        else
        {
            winner = "X";
            o_win_counter++;
        }

        MessageBox.Show(winner + " Wins!", "Winner");
    }
    else
    {
        if(turn_count==9)
            MessageBox.Show("Draw", "Draw!");


    }
}

private void disableButtons()
{
    try
    {
        foreach (Control control in controlGrid.Children)
        {
            if (control is Button)
            {
                Button button = (Button)control;
                button.IsEnabled = false;
            }
        }
    }
    catch { }
}

private void newButton_Click(object sender, RoutedEventArgs e)
{
    turn = true;
    turn_count = 0;

    try
    {
        foreach (Control control in controlGrid.Children)
        {
            if (control is Button)
            {
                Button button = (Button)control;
                button.IsEnabled = true;
                button.Content = "";
            }
        }
    }
    catch { }
}

private void button_enter(object sender, MouseEventArgs e)
{
    Button button = (Button)sender;
    if (button.IsEnabled)
    {
        if (turn)
            button.Content = "X";
        else
            button.Content = "O";
    }
}

private void button_leave(object sender, MouseEventArgs e)
{
    Button button = (Button)sender;
    if (button.IsEnabled)
        button.Content = "";



   } 
  }
}

主窗口.xaml

<Window x:Class="FirstApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="525" Name="MyWindow">        

<Grid Margin="0,0,0.4,-19.2" Name="controlGrid">
    <Button x:Name="A" Content="" HorizontalAlignment="Left" Margin="117,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="B" Content="" HorizontalAlignment="Left" Margin="223,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="C" Content="" HorizontalAlignment="Left" Margin="330,59,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="D" Content="" HorizontalAlignment="Left" Margin="117,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="E" Content="" HorizontalAlignment="Left" Margin="223,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="F" Content="" HorizontalAlignment="Left" Margin="330,157,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="G" Content="" HorizontalAlignment="Left" Margin="117,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="H" Content="" HorizontalAlignment="Left" Margin="223,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <Button x:Name="I" Content="" HorizontalAlignment="Left" Margin="330,255,0,0" VerticalAlignment="Top" Width="75" Height="75" Click="button_click" Background="#FFEBEB33" MouseEnter="button_enter" MouseLeave="button_leave" FontSize="36"/>
    <ToolBar Margin="0,0,0,450.6">
        <Button Name="newButton" Click="newButton_Click">New</Button>
    </ToolBar>
    <Label Content="X Win Count" HorizontalAlignment="Left" Margin="117,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label Content="Draw Count" HorizontalAlignment="Left" Margin="233,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label Content="O Win Count" HorizontalAlignment="Left" Margin="348,369,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
    <Label x:Name="x_win_count" Content="0" HorizontalAlignment="Left" Margin="138,400,0,0" VerticalAlignment="Top"/>
    <Label x:Name="draw_count" Content="0" HorizontalAlignment="Left" Margin="259,400,0,0" VerticalAlignment="Top"/>
    <Label x:Name="o_win_count" Content="{Binding Path=_o_win_counter, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="381,400,0,0" VerticalAlignment="Top"/>
</Grid>

最佳答案

替换

Content="{Binding Path=_o_win_counter, ...}"

Content="{Binding Path=o_win_counter, ...}"

没有前导下划线。绑定(bind)源必须是属性,而不是支持字段。您也可以重新考虑您的属性名称。 C# 中的属性名称使用 CamelCasing 已被广泛接受。

然后您还必须指定绑定(bind)的源对象,在本例中为 MainWindow 实例。有几种方法可以做到这一点,但这里最简单的方法可能是设置窗口的 DataContext 属性:

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

您可能想阅读 Data Binding Overview MSDN 上的文章。

关于c# - WPF 将标签内容绑定(bind)到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070095/

相关文章:

c# - 更改 XAML 中的绑定(bind)值

wpf - DataGridComboBoxColumn 中的 "An inline collection of ComboBoxItem"

c# - 如何在vs2010中开发64位版office的Microsoft office word addin

c# - 绑定(bind)到属性

c# - 使用暴露的 API 在 MVVM 应用程序中进行大量包装

c# - 基本的 gongsolution sample 没有掉落

c# - 有没有办法使用适用于 .NET 的 AWS 开发工具包将对象从 S3 存储桶下载到文件目标?

c# - 从数据表(Windows 窗体)填充 ListView 或列表框

javascript - 连续文本框验证

c# - 在标签中添加度数符号