Wpf 问题:带有样式的文本框不会在文本框中获取值。 (它总是空字符串)

问题描述 投票:0回答:1

我是 wpf 编程新手。当我点击按钮时。它应该向我显示文本框中的消息。但是,它仅显示一个空字符串。我的文本框使用名为“ModernTextBox”的自定义样式。

这是我的 xaml 文本框

<TextBox Grid.Row="1" Grid.Column="1" Height="50"
         VerticalAlignment="Center" Style="{StaticResource ModernTextBox}" x:Name="NameInsertTextBox"  
         />
<Button Grid.Row="1" Grid.Column="2" x:Name="NameInsertButton" VerticalAlignment="Stretch"
        Background="{StaticResource LightColorBrush}" Content="Insert" FontSize="12" Foreground= "White" BorderThickness="0"
        Click="NameInsertButton_Click"></Button>

这是我的 ModernTextBox 样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}" x:Key="ModernTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border CornerRadius="10"
                            Background="AliceBlue"
                            >
                        <Grid>
                            <Rectangle StrokeThickness="1">
                                
                            </Rectangle>
                            <TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0" Background="Transparent"
                                     VerticalContentAlignment="Center" Padding="5" Foreground="{StaticResource AccentGreyColorBrush}" x:Name="SearchBox">
                            </TextBox>
                            <TextBlock IsHitTestVisible="False" Text="Input seperated with commas" VerticalAlignment="Center" HorizontalAlignment="Left"
                                       Margin="10,0,0,0" FontSize="11" Foreground="DarkGray" Grid.Column="1">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Text, ElementName=SearchBox}" Value="">
                                                <Setter Property="Visibility" Value="Visible">

                                                </Setter>
                                            </DataTrigger>
                                        </Style.Triggers>
                                        <Setter Property="Visibility" Value="Hidden">
                                        </Setter>
                                    </Style>
                                </TextBlock.Style>
                                
                            </TextBlock>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        
    </Style>
</ResourceDictionary>

最后,这是我的代码

private void NameInsertButton_Click(object sender, RoutedEventArgs e)
{
 
    MessageBox.Show(NameInsertTextBox.Text);
    //sqlButton(nameInsertTextBox, NameInsertTextBox, "Insert");

}

看来问题出在

<TextBox Margin="1" Text="{TemplateBinding Text}" BorderThickness="0" Background="Transparent"
         VerticalContentAlignment="Center" Padding="5" Foreground="{StaticResource AccentGreyColorBrush}" x:Name="SearchBox">
</TextBox>

在现代文本框中。但我还是没找到解决办法。

c# wpf data-binding textbox
1个回答
0
投票

只需更改:

<TextBox Text="{TemplateBinding Text}"

致:

<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text}"
© www.soinside.com 2019 - 2024. All rights reserved.