0

I'm making a small application using .NET Core 6 WPF. I'm trying to get user input from a TextBox, but it returns a value I set through 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.

I don't know how to fix this.

  • Well, the only intuitive anwer that fits your problem is the textbox instance in `var taskText = TaskTextBox.Text` not being the textbox you are inputting text into in the GUI. How and why precisely that could be, i can't tell, because you have not revealed enough of your code to connect the points, so to speak. Therefore, **edit** your question and add the necessary information about where in your GUI do you set up the button and its click handler, and how/where is the button in the GUI element hierarchy located with respect to the TextBox (or vice versa). –  Sep 18 '22 at 15:55
  • @MySkullCaveIsADarkPlace I edited my question. What can you say now? I'm sure that `TaskTextBox` is the TextBox i need. – Bohdan Petrenko Sep 18 '22 at 16:21
  • You are using `{TemplateBinding}` for binding the ControlTemplate's textbox. `{TemplateBinding}` is one way, with data only transferring to the binding target, i.e, the textbox inside the control template) but not the other way. See here: https://stackoverflow.com/questions/5913176/in-wpf-why-doesnt-templatebinding-work-where-binding-does for some advice of how you could hopefully make your Text template binding work. –  Sep 18 '22 at 16:41
  • @MySkullCaveIsADarkPlace Yes, changing `{TemplateBinding}` to `"{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"` helped. Now I get the correct value. You can make an answer so I mar question as answered. – Bohdan Petrenko Sep 18 '22 at 16:53
  • @BohdanPetrenko: A `ControlTemplate` of a `TextBox` should not contain another `TextBox`... – mm8 Sep 19 '22 at 14:02

1 Answers1

0

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.

So, on your underlying binding source DATACONTEXT, have a property like

public DateTime myDate { get; set;}

Then, in have a DatePicker control and bind the SelectedDate to the date property exposed. Then you get built-in calendar style features, can do black-out dates, and more.

FEEDBACK.

By looking at your sample XAML portion, it LOOKS like you are not doing MVVM pattern (better to learn/extend to that). It looks like your code is all lying within your YourClass.xaml (Visual component) and YourClass.xaml.cs (Code behind for action). I see this from your button binding of SaveButton_OnClick which recognizes the .xaml.cs for the source. This is basically the binding "Context" of the control that you are putting all your stuff into (textbox, button, labels, etc.).

So, all that being said, just add a public property as described in the same .xaml.cs that your SaveButton_OnClick is located. Just for grins, you have a textbox used for a date, but try doing a datepicker control. For grins, just to follow, add another textbox just for simple purposes of following the binding. Notice that on each, I put a default value, so when you run the form, you SHOULD see these values immediately just to KNOW the bindings are accurate.

public DateTime myDate { get; set;} = DateTime.Now.Date;

public int myIntProp { get; set;} = 32;

Now, in your .xaml file put something like

<DatePicker SelectedDate="{Binding myDate,
    UpdateSourceTrigger=PropertyChanged}" />

<TextBox Text="{Binding myIntProp}" />

Now, when you click on the button and check the values (after you have changed them to something other than their default upon creation), you SHOULD see the new values and in their proper data type respectively.

DRapp
  • 47,638
  • 12
  • 72
  • 142