1

私は持っていますButtonsWrapPanelそれらは動的に作成しています。Button特定のonの高さ/幅を変更したいClick_event

これが私がしていることです:

    for (int i = 1; i <= count; i++)
    {
                btn = new Button();
                btn.MinHeight = 22;
                btn.MinWidth = 22;

                btn.Content = i.ToString();
                int _id = id++;
                btn.Name = "btn"+_id.ToString();
                wrpQuestionsMap.Children.Add(btn);

                btn.Click += new RoutedEventHandler(btn_Click);
    }

    private void btnNext_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.view.CurrentPosition < this.view.Count - 1)
            {
                this.view.MoveCurrentToNext();

                Button b = (Button)this.wrpQuestionsMap.FindName("btn"+view.CurrentPosition.ToString());
                if (b != null)
                {
                    b.Width = 30;
                }
            }
        }

上記を試しましたが、null になっています。理由がわかりません。助けてください ありがとう

4

1 に答える 1

1

私が正しく理解し、クリックしたボタンのサイズを変更したい場合: このコード行の場合:

btn.Click += new RoutedEventHandler(btn_Click);

次のようなメソッドが必要です。

void btn_Click(object sender, RoutedEventArgs e)
{
  Button btn=(Button)sender; // this is the clicked Button
  btn.Width=30.0;            //changes its Width
}

編集:

foreach (Button btn in wrpQuestionsMap.Children)
{
    string name= btn.Content.ToString();
    if  (name == "yourName")   // yourName is the name you are searching for
    {
         btn.Width = 30.0   //change size
         break;             // no need to search more
    }
}

編集 2: 質問のコードから、あなたの Buttons の Content は number のようbtn.Content = i.ToString();です。あなたはコメントで、それview.CurrentPosition.ToString()があなたの現在の質問の番号だと言いました。このボタンの幅を変更する場合は、次を使用します。

foreach (Button btn in wrpQuestionsMap.Children)
{
    string name= btn.Content.ToString(); // it must be a number, check it in the debug, and if it is not, Let me know
    if  (name == view.CurrentPosition.ToString())
    {
         btn.Width = 30.0   //change size
         break;             // no need to search more
    }
}

別のボタンの幅を変更したい場合は、そのボタンに書かれている内容をお知らせください。

于 2012-12-25T06:39:59.477 に答える