0

これと同じ属性を持つコードビハインドからボタンを作成しようとしています:

<Button Content="Find Student" Tag="FindStudent" HorizontalAlignment="Right" x:Name="btnFindStudent" Click="btnGeneral_Click" />

背後にあるコード:

            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View");
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

問題は、ボタンをクリックするとエラーが発生することです

Value cannot be null.
Parameter name: Children of 'System.Windows.Controls.UIElementCollection' cannot be null. Object derived from UIElement expected.

そして、何が欠けているのかよくわかりません。これは完全なコードです。

public partial class FindStudent : UserControl
{
    private Dictionary<string, UserControl> _userControls = new Dictionary<string, UserControl>();
    public Dictionary<string, UserControl> GetUserControls()
    {
        return _userControls;
    }
    private static readonly Random rand = new Random();
    public FindStudent()
    {
        InitializeComponent();

        List<string> userControlKeys = new List<string>();
        userControlKeys.Add("FindStudent");
        Type type = this.GetType();
        Assembly assembly = type.Assembly;
        foreach (string userControlKey in userControlKeys)
        {
            string userControlFullName = String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey);
            UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
            _userControls.Add(userControlKey, userControl);
        }
        for (int i = 1; i <= 10; i++)
        {
            Button btnFindStudent = new Button();
            btnFindStudent.Click += this.btnGeneral_Click;
            btnFindStudent.Name = Convert.ToString("btnFindStudent");
            btnFindStudent.Tag = Convert.ToString("FindStudent");
            btnFindStudent.Content = Convert.ToString("View {0}", i);
            btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
            btnFindStudent.Height = 20;
            btnFindStudent.Width = 36;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(btnFindStudent);
            MainArea.Children.Add(stackPanel);
        }
    }
    private void btnGeneral_Click(object sender, RoutedEventArgs e)
    {
        PanelMainContent.Children.Clear();
        Button button = (Button)e.OriginalSource;
        Type type = this.GetType();
        Assembly assembly = type.Assembly;

        PanelMainContent.Children.Add(_userControls[button.Tag.ToString()]);
        // error on above line. 
    }
}

Xaml:

<UserControl x:Class="WpfApplication4.AppPages.FindStudent"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel HorizontalAlignment="Stretch" >
        <ScrollViewer VerticalScrollBarVisibility="Hidden">

            <DockPanel x:Name="PanelMainWrapper" Margin="10,20,0,0">
                <DockPanel x:Name="PanelMainContent" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,30,0,0">
                    <ScrollViewer VerticalScrollBarVisibility="Hidden">
                        <WrapPanel x:Name="MainArea" VerticalAlignment="Bottom" HorizontalAlignment="Left">

                        </WrapPanel>
                    </ScrollViewer>
                </DockPanel>
            </DockPanel>

        </ScrollViewer>
    </DockPanel>
</UserControl>

現在使用しているページをリロードするだけで、後日appPageを設定できますが、ボールをプレイしたくありません。これは私のコントロールではなくメインウィンドウで機能しますが、メインウィンドウで最初に言及したxamlを使用するコードビハインドからボタンを設定しません。これは、ボタンクリックから何かが欠落していることを示しています(正しく設定されていると思います)、タグは正しく設定されていますが、名前がxamlコードのようにx:Nameになるかどうかはわかりません。

4

1 に答える 1

1

あなたのコードは本当に私を混乱させますが、これが機能していないものです。によって完全修飾型名を作成する

String.Format("{0}.AppPages.{1}", type.Namespace, userControlKey)

問題のタイプの完全な名前空間がtype.Namespaceすでに含まれているため、「AppPages」の部分は不要です。代わりに、あなたは書くべきです

String.Format("{0}.{1}", type.Namespace, userControlKey)

そして、なぜ単純にしないのですか

assembly.CreateInstance(type.FullName);

の新しいインスタンスのみを作成したい場合FindStudent(これはあなたがやりたいことだと思います)。

于 2012-04-17T11:49:11.690 に答える