FrameworkElement.FindName 根據名字查找控件

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

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


參考:api

章節:app


正文:學習

最近寫個小玩意,原本是想用 TabControl 標明標籤,而後 TabItem 作容器裏面放些控件,可是我 TabItem裏面的控件徹底同樣,用 TabItem 來作容器彷佛太笨重了,因而想用個 StackPanel 來放標籤,其餘控件用個 Grid 來包裹就成了。測試的時候發現 FindName() 找不到後臺程序生成的 RadioButton。程序以下:測試

Xaml 界面程序url

<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>

spa

C# 後臺程序: 3d

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);
        }
    }
}
orm

運行效果以下:

可是刪除的時候,發現 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);
}

運行效果以下:

相關文章
相關標籤/搜索