我怎样才能得到文本框已被pressed持何立场? [英] How can I get the Position of textbox that has been pressed?

查看:170
本文介绍了我怎样才能得到文本框已被pressed持何立场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在WPF的数独游戏,我做的运行时间81文本框在画布上:

I am writing in WPF the Sudoku Game and i make in run time 81 textboxes on canvas:

public partial class Test : Window
    {
        private TextBox[,] texts = new TextBox[9, 9];
        GameBoard board = new GameBoard();

    public Test(string path)
    {
        InitializeComponent();
        Initialization_text();
    }

    void Initialization_text()
    {
        for (int i = 0; i < texts.GetLength(0); i++)
        {
            for (int j = 0; j < texts.GetLength(1); j++)
            {
                texts[i, j] = new TextBox();
                texts[i, j].Name = "txt" + i + j;
                texts[i, j].Width = 40;
                texts[i, j].Height = 40;
                texts[i, j].MaxLength = 1;
                texts[i, j].FontSize = 22;
                texts[i, j].FontWeight = FontWeights.Bold;
                texts[i, j].Foreground = new SolidColorBrush(Colors.DimGray);
                texts[i,j].TextAlignment = TextAlignment.Center;
                Canvas.SetLeft(texts[i, j], (i+1)*40);
                Canvas.SetTop(texts[i, j], (j+1)*40);
                canvas1.Children.Add(texts[i, j]);
            }
        }
    }

我需要得到文本框的用户输入的号码用于检查值的位置,但我不能写称为TextBoxKeyDown的方法,因为它在运行时生成

I need get the position of textbox that user is enter the number for checking the value, but i cant write the method that called TextBoxKeyDown because its make in run time

但如果我写这个方法:

private void canvas1_KeyDown(object sender, KeyEventArgs e)
        {
            if (sender.GetType().Name == "TextBox")//the sender is canvas
            {

            }
        }

我怎样才能在文本框的用户输入数字? 请帮助...

how can i get the textbox that user is enter the number? pls help...

推荐答案

确定。删除所有code,一切重新开始。

Ok. Delete all your code and start all over.

这是你如何做一个数独板在WPF:

This is how you do a Sudoku board in WPF:

XAML:

<Window x:Class="WpfApplication4.Window17"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window17" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="9" Columns="9"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Row" Value="{Binding Row}"/>
                <Setter Property="Grid.Column" Value="{Binding Column}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Value}" VerticalAlignment="Stretch" FontSize="20" TextAlignment="Center"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

code背后:

Code Behind:

using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;

namespace WpfApplication4
{
    public partial class Window17 : Window
    {
        public Window17()
        {
           InitializeComponent();

           var random = new Random();

           var board = new List<SudokuViewModel>();

           for (int i = 0; i < 9; i++)
           {
               for (int j = 0; j < 9; j++)
               {
                   board.Add(new SudokuViewModel() {Row = i, Column = j,Value = random.Next(1,20)});
               }
           }

           DataContext = board;            
       }
   }
}

视图模型:

 public class SudokuViewModel:INotifyPropertyChanged
    {
        public int Row { get; set; }

        public int Column { get; set; }

        private int _value;
        public int Value
        {
            get { return _value; }
            set
            {
                _value = value;
                NotifyPropertyChange("Value");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

    }

正如你所看到的而言,我没有办法创建或操纵在code UI元素。这是完全错误的WPF中。你必须学会​​MVVM,并了解用户界面是不是数据。数据是。用户界面是用户界面

现在,每当你需要对在文本框的值进行操作,只是操作对公共int值在视图模型属性。您的应用程序逻辑和你的用户界面必须完全脱钩。

Now, whenever you need to operate against the Value in the TextBoxes, just operate against the public int Value property in the ViewModel. Your application logic and your UI must be completely decoupled.

只需复制和粘贴我的code在文件 - &GT;新建项目 - &GT; WPF应用程序,看到的结果吧。这是它的外观在我的电脑:

Just copy and paste my code in a File -> New Project -> WPF Application and see the results for yourself. This is how it looks in my computer:

编辑:

我已经修改了样品称当值改变的方法。

I have modified the sample to call a method when a Value is changed.

请理解,你不应该对在WPF用户界面进行操作,而是针对数据。 你真正关心的是数据(视图模型),而不是UI本身。

Please understand that you should NOT operate against the UI in WPF, but against DATA. What you really care about is DATA (The ViewModel), not the UI itself.

using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System;

namespace WpfApplication4
{
    public partial class Window17 : Window
    {
        public List<SudokuViewModel> Board { get; set; } 

        public Window17()
        {
            InitializeComponent();

            var random = new Random();

            Board = new List<SudokuViewModel>();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Board.Add(new SudokuViewModel()
                                  {
                                      Row = i, Column = j,
                                      Value = random.Next(1,20),
                                      OnValueChanged = OnItemValueChanged
                                  });
                }
            }

            DataContext = Board;
        }

        private void OnItemValueChanged(SudokuViewModel vm)
        {
            MessageBox.Show("Value Changed!\n" + "Row: " + vm.Row + "\nColumn: " + vm.Column + "\nValue: " + vm.Value);
        }
    }

    public class SudokuViewModel:INotifyPropertyChanged
    {
        public int Row { get; set; }

        public int Column { get; set; }

        private int _value;
        public int Value
        {
            get { return _value; }
            set
            {
                _value = value;
                NotifyPropertyChange("Value");

                if (OnValueChanged != null)
                    OnValueChanged(this);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

        public Action<SudokuViewModel> OnValueChanged { get; set; }

    }
}

这篇关于我怎样才能得到文本框已被pressed持何立场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆