从数据网格堆栈面板 WPF 中的文本框输入

Input from textbox in datagrid stack panel WPF

如何从文本框 tbUser 获取代码隐藏输入? 我需要在后面的代码中使用该输入,但出现 tbUser 不存在 .

的错误
<DataGridTextColumn Binding="{Binding User Name}" Width="130" MinWidth="130" MaxWidth="150"> 
    <DataGridTextColumn.HeaderTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBox BorderBrush="Black" BorderThickness="1" Margin="1"
                    Name="tbUser" Width="117" MinWidth="117" MaxWidth="147"/>
                <TextBlock Text="User Name" Margin="1" FontWeight="DemiBold"
                    TextWrapping="Wrap" TextAlignment="Left"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTextColumn.HeaderTemplate>
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
            <Setter Property="TextBlock.TextAlignment" Value="Left"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

您可以将 DataGridTextColumn.HeaderTemplate 更改为 DataGridTextColumn.Header,因为您仅将此模板用于此列,而不会覆盖 ControlTemplate

<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding UserName}" Width="130" MinWidth="130" MaxWidth="150">
        <DataGridTextColumn.Header>
            <StackPanel>
                <TextBox BorderBrush="Black" BorderThickness="1" Margin="1" Name="tbUser" Width="117" MinWidth="117" MaxWidth="147"/>
                <TextBlock Text="User Name" Margin="1" FontWeight="DemiBold"
            TextWrapping="Wrap" TextAlignment="Left"/>
            </StackPanel>
        </DataGridTextColumn.Header>
        <DataGridTextColumn.ElementStyle>
            <Style>
                <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                <Setter Property="TextBlock.TextAlignment" Value="Left"/>
            </Style>
        </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

存在绑定错误Binding="{Binding User Name}"。你是说 Binding="{Binding UserName}"(不是 space)?