1. 程式人生 > >Framework?Element.?Find?Name 根據名字查找控件

Framework?Element.?Find?Name 根據名字查找控件

mage sts radio names reg url soft ems rgs

WPF Framework?Element.?Find?Name 根據名字查找控件

運行環境:Window7 64bit,NetFramework4.7,C# 7.0, 編者:烏龍哈裏 2017-10-04


參考:

  • Framework?Element.?Find?Name Method
  • Framework?Element.?Register?Name Method

章節:


正文:

最近寫個小玩意,本來是想用 TabControl 標明標簽,然後 TabItem 做容器裏面放些控件,但是我 TabItem裏面的控件完全一樣,用 TabItem 來做容器似乎太笨重了,於是想用個 StackPanel 來放標簽,其他控件用個 Grid 來包裹就成了。測試的時候發現 FindName()

找不到後臺程序生成的 RadioButton。程序如下:

Xaml 界面程序

<Window x:Class="學習FindName.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="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"


mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="6*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<ListBox Name="lstShow" Grid.Row="0"/>
<StackPanel Name="stackpanel" Orientation="Horizontal" Grid.Row="1"/>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button Name="btnAdd" Content="添加" Margin="3" Grid.Column="0" Click="btnAdd_Click" />
<Button Name="btnDrop" Content="刪除" Margin="3" Grid.Column="1" Click="btnDrop_Click" />
</Grid>
</Grid>
</Window>

C# 後臺程序:

using System.Windows;
using System.Windows.Controls;

namespace 學習FindName
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
private int RadioButtonNum = 0;

public MainWindow()
{
InitializeComponent();
}
//---添加 radiobutton
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
RadioButtonNum++;
string s = "rbn" + RadioButtonNum.ToString();
RadioButton rbn = new RadioButton{ Name = s, Content = s,Margin=new Thickness(3)};
rbn.Click += RadioButton_Click;
stackpanel.Children.Add(rbn);
}
//---刪除 radiobutton
private void btnDrop_Click(object sender, RoutedEventArgs e)
{
string s = "rbn" + RadioButtonNum.ToString();
RadioButton rbn = stackpanel.FindName(s) as RadioButton;
stackpanel.Children.Remove(rbn);
}
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
//(sender as RadioButton).IsChecked = true;
string s = (sender as RadioButton).Name;
RadioButtonNum = int.Parse(s[s.Length - 1].ToString());
lstShow.Items.Add(s);
}
}
}

運行效果如下:

技術分享

但是刪除的時候,發現 FindName() 返回的值為 null,沒有找到控件。找了半天資料,才發現不在前臺 Xaml 裏面定義的控件名字,要用 ?Register?Name() 方式來註冊一下名字,後面的 FindName() 才能找到。
更改程序如下:

//---添加 radiobutton
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
RadioButtonNum++;
string s = "rbn" + RadioButtonNum.ToString();
RadioButton rbn = new RadioButton{ Name = s, Content = s,Margin=new Thickness(3)};
rbn.Click += RadioButton_Click;
stackpanel.Children.Add(rbn);
//註冊一下名字,沒有這句後面的 FindName() 將找不到控件
stackpanel.RegisterName(s, rbn);
}
//---刪除 radiobutton
private void btnDrop_Click(object sender, RoutedEventArgs e)
{
string s = "rbn" + RadioButtonNum.ToString();

RadioButton rbn = stackpanel.FindName(s) as RadioButton;
rbn.Click -= RadioButton_Click;//事件如果不註銷,容易引起內存泄漏
stackpanel.Children.Remove(rbn);
}
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
//(sender as RadioButton).IsChecked = true;
string s = (sender as RadioButton).Name;
RadioButtonNum = int.Parse(s[s.Length - 1].ToString());
lstShow.Items.Add(s);
}

運行效果如下:

技術分享

Framework?Element.?Find?Name 根據名字查找控件