从datagrid堆栈面板WPF中的文本框输入

时间:2016-03-07 10:24:15

标签: c# wpf datagrid stackpanel

如何从textbox 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>

1 个答案:

答案 0 :(得分:0)

您可以将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}"(没有空格)?