Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding.oneWayToSource #55

Closed
ChernayaKoshka opened this issue Sep 19, 2018 · 3 comments
Closed

Binding.oneWayToSource #55

ChernayaKoshka opened this issue Sep 19, 2018 · 3 comments

Comments

@ChernayaKoshka
Copy link

Hello,

When working with textboxes in WPF, I continue to come across an issue. If I am typing too fast in a textbox control bound like so:

<TextBox Text="{Binding Path=ImportantText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        "ImportantText"
        |> Binding.twoWay
            (fun m -> m.ImportantText)
            (fun it _ -> it |> ImportantText)

Then there comes an issue where the Elmish.WPF is still updating the UI in the backend while the user is still entering data. What this means is, the users' caret goes to the beginning of the textbox, causing them to "type behind" the newly updated text. For an example of this issue, you can type quickly in the NewWindow sample.

I feel that the solution is to support Binding.oneWayToSource, which would mean that the code behind has no impact on the view (as there is no point to it anyway). Unless, of course, there is already a solution implemented that I am unaware of.

In the meantime, assuming that there is no particular reason the view needs to be updated as the user is typing, then a potential solution is to use WPF with InputBindings like so:

<TextBox Text="Test">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding ImportantText}" Key="Return" />
        <KeyBinding Command="{Binding ImportantText}" Key="Enter" />
    </TextBox.InputBindings>
</TextBox>

Or a command binding attached to a "submit" button.

Thoughts?

@cmeeren
Copy link
Member

cmeeren commented Sep 19, 2018

This is indeed a problem if you use UpdateSourceTrigger=PropertyChanged. I have previously discussed the underlying issue at length in #40. As far as I know, there is no good way to fix this due to the underlying issue being entirely general in nature.

I haven't tested, but I think setting Mode=OneWayToSource should work when you have defined Binding.twoWay. In any case, setting Mode=OneWayToSource means that the model can not easily control what is shown in the TextBox. This might be a problem for setting initial values in the fields, but there might be other ways around that. I haven't thought too hard about it yet.

Another quick solution if you don't need to update anything as you type, is simply to remove UpdateSourceTrigger=PropertyChanged. It will then update when the TextBox loses focus.

@cmeeren
Copy link
Member

cmeeren commented Sep 19, 2018

For an example of this issue, you can type quickly in the NewWindow sample.

Note that there's a big difference between Debug and Release modes, particularly if you have any kind of Elmish.WPF logging turned on (Program.withConsoleTrace or an ElmConfig with logging). I experience this issue all the time with logging on, but in Release mode without any logging, I have yet to experience this.

@ChernayaKoshka
Copy link
Author

It appears setting the binding mode to OneWayToSource and using binding.twoWay has solved my problem completely. The performance difference between debug/release modes is good to keep in mind. I appreciate your timely response. Given this is a duplicate of #40 I will go ahead and close this and continue any further discussion in that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants