简体   繁体   English

WPF TextBox.Text 返回默认值

[英]WPF TextBox.Text returns default value

I'm making a small application using .NET Core 6 WPF.我正在使用 .NET Core 6 WPF 制作一个小型应用程序。 I'm trying to get user input from a TextBox, but it returns a value I set through XAML.我试图从 TextBox 获取用户输入,但它返回我通过 XAML 设置的值。 This is how I declare a TextBox:这就是我声明文本框的方式:

<TextBox Name="TaskTextBox" Margin="0, 0, 0, 20" Text="Task" Style="{StaticResource InputFieldStyle}"/>
<Button 
    Name="SaveButton"
    Click="SaveButton_OnClick"
    Style="{StaticResource HomeTaskEditButtonStyle}"
    Background="#5abf26"
    HorizontalAlignment="Left"
    Width="100">
       <TextBlock
            Text="Save"
            Foreground="White"
            FontSize="20"/>
</Button>

And here is how I try to get the input:这是我尝试获取输入的方式:

private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
    var taskText = TaskTextBox.Text; // Returns "Task", But I entered "Test"
    var lessonText = LessonTextBox.Text;
        
    // Next Logic
}

And here is my TextBox Style:这是我的文本框样式:

    <Style x:Key="InputFieldStyle" TargetType="TextBox">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Border
                        CornerRadius="10"
                        Background="#464646"
                        Padding="5">
                        <TextBox
                            Text="{TemplateBinding Text}"
                            FontSize="16"
                            Margin="1"
                            BorderThickness="0"
                            Background="Transparent"
                            Padding="5"
                            Foreground="White"
                            TextWrapping="WrapWithOverflow"
                            CaretBrush="White"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I request TaskTextBox.Text when the button is pressed.我在按下按钮时请求TaskTextBox.Text

I don't know how to fix this.我不知道如何解决这个问题。

When dealing with dates, use date properties on whatever your binding source.处理日期时,请在任何绑定源上使用日期属性。 Same with other data types like int, decimal, float, etc. If you set the textbox control to a binding of a specific type, or a date picker control to a date field respectively, YOU dont have to worry as much about a valid value or type conversion.与 int、decimal、float 等其他数据类型相同。如果将文本框控件设置为特定类型的绑定,或者将日期选择器控件分别设置为日期字段,则不必担心有效值或类型转换。

So, on your underlying binding source DATACONTEXT, have a property like因此,在您的基础绑定源 DATACONTEXT 上,具有类似的属性

public DateTime myDate { get;公共日期时间 myDate { 获取; set;}放;}

Then, in have a DatePicker control and bind the SelectedDate to the date property exposed.然后,拥有一个 DatePicker 控件并将 SelectedDate 绑定到公开的日期属性。 Then you get built-in calendar style features, can do black-out dates, and more.然后,您将获得内置的日历样式功能,可以设置不适用日期等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM