Как обратиться к свойству из private void - C#
Формулировка задачи:
Есть WPF окно с кнопкой и надписью
и есть класс с одним публичным свойством Amount в котором просто планирую хранить число типа int
Проблема в том что при попытке компиляции получаю ошибку вида "The name 'N1' does not exist in the current context". Как правильно работать с свойством из private void button1_Click ?
<Window x:Class="SvoistvoClic.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">
<Grid>
<StackPanel Name="stackPanel1">
<Label Content="Надпись 1" Height="28" Name="label1" />
<Button Content="Button" Name="button1" HorizontalAlignment="Left" Click="button1_Click" />
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 SvoistvoClic
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataLabel N1 = new DataLabel();
N1.Amount = 2;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (N1.Amount == 2){
label1.Content = "Надпись 2";}
}
}
class DataLabel
{
public int Amount { get; set; }
}
}Решение задачи: «Как обратиться к свойству из private void»
textual
Листинг программы
DataLabel N1 = new DataLabel();
public MainWindow()
{
InitializeComponent();
N1.Amount = 2;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (N1.Amount == 2){//переменная видна
label1.Content = "Надпись 2";}
}