0

I am trying to add delay of one second as the user is typing the word to search in the search box rather than updating it continuously as he is typing. I know that there is a "Delay" keyword that can be used but this requires to hardcode a value into the xaml file.

I want to be able to read the delay interval from the configuration file. I have define a delay value in the Settings.settings and want to be able to do something like the following;

In the xaml file, I have something like the following:

Text="{Binding myText, Source={StaticResource queryView}, 
UpdateSourceTrigger=PropertyChanged, 
Delay=Property.Settings.Default.DelayValue}"

I want to do be able to set the Delay from the configuration file like the way shown in the code.

I am unable to set it and am very new to C#. Can someone guide me on how to be able to set the Delay without hardcoding it. The current approach is failing. :(

Thanks.

Laura Smith
  • 293
  • 3
  • 13

1 Answers1

0

Try this answer : https://stackoverflow.com/a/845033/4117068

Basically Declare a XML namespace with yourNamespace.Properties

xmlns:properties="clr-namespace:YourNamespace.Properties"

Then call your setting with

{x:Static properties:Settings.DelayValue}

So with your piece of code above this should look like this :

Text="{Binding myText, Source={StaticResource queryView}, 
UpdateSourceTrigger=PropertyChanged, 
Delay={x:Static properties:Settings.DelayValue}}"

EDIT: You could also do it from the code behind in the xaml.cs file.

To do that, Give an X:Name to your TextBox

<TextBox x:Name="yourTextBox"  ... />

Then in the code behind. In the constructor after the InitializeComponent(); Get the Binding of the textBox and set the Delay like this :

BindingExpression bindingExpression = yourTextBox.GetBindingExpression(TextBox.TextProperty); 
Binding parentBinding = bindingExpression.ParentBinding;
parentBinding.Delay = Properties.Settings.Default.DelayValue;
yourTextBox.SetBinding(TextBox.TextProperty, parentBinding);
Leojet
  • 61
  • 1
  • 8
  • I tried to use the modification as you suggested by I get the error that "Cannot find the static member 'DelayValue' on the type 'Settings'. I see that it is defined in the Settings.settings file – Laura Smith Dec 20 '18 at 20:53
  • @LauraSmith Does the Output Windows says anything else? This seem to be a very generic error. Maybe something is wrong with your settings. It's hard to tell since I am also kinda new to C#. – Leojet Dec 20 '18 at 22:14
  • @LauraSmith, You could also try to set the Settings.settings Access Modifier to Public. It's on the top of the settings page. If you find a way to fix this error let me know, I'm curious – Leojet Dec 20 '18 at 22:22
  • I set it to public, but nothing changes. Do you know anything other approach? Any help would be appreciated. – Laura Smith Dec 21 '18 at 13:54
  • @LauraSmith See my edit, I managed to do it in code behind. Both ways work in my application... – Leojet Dec 21 '18 at 15:06
  • When I do the parentBinding.Delay = Properties.Settings.Default.DelayValue, i get the exception error. It seems to be breaking at that code line. For the assignement of Delay={x:Static properties:Settings.DelayValue}}" in the xaml file, I only see Settings.Default when I type and not DelayValue. I set it to Public but still not working. May you please help me resolve this? Thank you so much as it means alot to me. – Laura Smith Dec 21 '18 at 16:23
  • To mention, I am using a AutoCompleteTextBox. – Laura Smith Dec 21 '18 at 16:33
  • I forgot to mention, if you are setting the Delay from code behind. Remove it from the xaml. I did not realized it was a AutoCompleteTextBox, my bad. I'll try to reproduce it using that. – Leojet Dec 21 '18 at 16:38
  • I removed it from the xml and tried to set it but still got the exception at parentBindind.Delay. Thank you for your continous help – Laura Smith Dec 21 '18 at 16:41
  • What is the exception type, and exception message ? – Leojet Dec 21 '18 at 16:53
  • InvalidOperationException: Binding cannot be changed after it has been used. – Laura Smith Dec 21 '18 at 17:08
  • error MC3011: Cannot find the static member 'DelayValue' on the type 'Settings'. I get this when I use the Delay={x:Static properties:Settings.DelayValue}}". Do you also get the same? @Leojet – Laura Smith Dec 21 '18 at 18:11