无法检索动态创建的textBox的文本

时间:2017-04-05 11:26:05

标签: c# wpf xaml

我跟着这个和其他链接: How to retrieve the text of a dynamically created textbox and display its as a content of dynamically created button in wpf c#

但对我来说它不起作用。 我在这里生成我的TextBlock:

TextBox NameBox = new TextBox();
NameBox.Text = expander.Header.ToString();
MaterialDesignThemes.Wpf.HintAssist.SetHint(NameBox, "Rolename");
MaterialDesignThemes.Wpf.HintAssist.SetIsFloating(NameBox, true);
NameBox.Opacity = .68;
NameBox.Width = 200;
NameBox.Name = "roleNameBox";
DockPanel.SetDock(NameBox, Dock.Left);

及其成功。但是,如果我尝试通过生成的Button获取Text:

Button saveBtn = new Button();
saveBtn.Name = "SaveRoleBtn";
saveBtn.Style = (Style)FindResource("MaterialDesignToolButton");
saveBtn.Margin = new Thickness(0, 0, 8, 0);
saveBtn.ToolTip = "Save Role";
saveBtn.Height = 24;
saveBtn.Width = 24;
saveBtn.Click += SaveRoleBtn_Click;
MaterialDesignThemes.Wpf.RippleAssist.SetIsCentered(saveBtn, true);
DockPanel.SetDock(saveBtn, Dock.Right);

事件:

private void SaveRoleBtn_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    var parent = button.Parent as FrameworkElement;
    var roleName = parent.FindName("roleNameBox") as TextBox;

    if (roleName != null)
        MessageBox.Show(roleName.Text);
    else
        MessageBox.Show("Is null");
}

每次都是null。这怎么可能? 'as FrameWorkElement;'错了吗?我还有什么要放在那里呢?

如果不生成代码,这将是XAML:

<ScrollViewer Grid.Row="1">
        <materialDesign:Card Grid.Row="1" Background="{DynamicResource MaterialDesignBackground}">
            <StackPanel x:Name="roleListSp" Grid.Row="1">
                <!-- HERE STARTS GENERATED CONTROLS -->
                <Expander HorizontalAlignment="Stretch" Header="Rolle 1">
                    <StackPanel Orientation="Vertical" TextBlock.Foreground="{DynamicResource MaterialDesignBody}" Margin="24,8,24,16">
                        <DockPanel LastChildFill="False">
                            <Button x:Name="saveRoleBtn" Style="{StaticResource MaterialDesignToolButton}" DockPanel.Dock="Right" Margin="0 0 8 0" HorizontalAlignment="Left" ToolTip="Save Role" Width="24" Height="24" materialDesign:RippleAssist.IsCentered="True" Click="SaveRoleBtn_Click">
                                <materialDesign:PackIcon Kind="ContentSave" Height="16" Width="16" />
                            </Button>
                            <Button x:Name="deleteRoleBtn" Style="{StaticResource MaterialDesignToolButton}" DockPanel.Dock="Right" Margin="0 0 8 0" HorizontalAlignment="Left" ToolTip="Delete Role" Width="24" Height="24" materialDesign:RippleAssist.IsCentered="True" Click="deleteRoleBtn_Click">
                                <materialDesign:PackIcon Kind="Delete" Height="16" Width="16" />
                            </Button>
                            <TextBox x:Name="roleNameBox" Opacity=".68" materialDesign:HintAssist.Hint="Role Name" Width="200" materialDesign:HintAssist.IsFloating="True"/>
                        </DockPanel>
                        <TextBox x:Name="roleDescBox" Opacity=".68" materialDesign:HintAssist.Hint="Description" materialDesign:HintAssist.IsFloating="True" TextWrapping="Wrap" MouseDown="Beschreibung_MouseDown"/>
                        <TextBlock Text="Rights" Margin="0,10,0,0" />
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="0">
                                <DockPanel LastChildFill="False">
                                    <TextBlock Opacity=".68" Text="Can Add:" DockPanel.Dock="Left" />
                                    <ToggleButton DockPanel.Dock="Right" PreviewMouseDown="ToggleButton_PreviewMouseDown"/>
                                </DockPanel>
                                <DockPanel Margin="0,2,0,0" LastChildFill="False">
                                    <TextBlock Opacity=".68" Text="Can Edit:" DockPanel.Dock="Left" />
                                    <ToggleButton DockPanel.Dock="Right" PreviewMouseDown="ToggleButton_PreviewMouseDown" />
                                </DockPanel>
                            </StackPanel>
                            <StackPanel Grid.Column="1">
                                <DockPanel LastChildFill="False">
                                    <TextBlock Opacity=".68" Text="Can Delete:" DockPanel.Dock="Left"/>
                                    <ToggleButton IsChecked="True" DockPanel.Dock="Right" PreviewMouseDown="ToggleButton_PreviewMouseDown"/>
                                </DockPanel>
                                <DockPanel LastChildFill="False" Margin="0,2,0,0">
                                    <TextBlock Opacity=".68" Text="Administration:" DockPanel.Dock="Left" />
                                    <ToggleButton DockPanel.Dock="Right" PreviewMouseDown="ToggleButton_PreviewMouseDown" />
                                </DockPanel>
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                </Expander>
                <!-- HERE ENDS GENERATED CONTROLS -->
            </StackPanel>
        </materialDesign:Card>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:1)

如果您将TextBox添加到与DockPanel相同的Button,您可以在DockPanel的{​​{1}}集合中查找Children

private void SaveRoleBtn_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    var parent = button.Parent as DockPanel;
    if(parent != null)
    {
        var textBox = parent.Children.OfType<TextBox>().FirstOrDefault();
        if (textBox != null)
            MessageBox.Show(textBox.Text);
        else
            MessageBox.Show("Is null");
    }
}

如果TextBox可能位于不同的子面板中,您可以使用在可视树中搜索它的辅助方法:

How can I find WPF controls by name or type?

var textBox = FindChild<TextBox>(roleListSp, "roleNameBox");