TextBox onchange event is frining twice with placeholder in WPF - c#

I have added a placeholder to the Textbox using below link,
http://www.techken.in/coding/c-wpf-create-textbox-placeholder-using-xaml-code/
Below is the textbox syntax i used,
<TextBox Style="{StaticResource placeHolder}" Tag="Input text" Text="
{Binding Path=Token,Mode=OneWayToSource,
UpdateSourceTrigger=PropertyChanged}" cal:Message.Attach="[Event
TextChanged] = [Action OnChangeEvent()]" />
When placeHolder is kept event is firing twice. If I remove UpdateSourceTrigger for Textbox which is inside of placeHolder xaml I could not get the changed property value. Anyone please let me know how I can make this to hit OnChangeEvent once only with placeholder.

Try getting the value in the property setter so the property is not set twice causing the OnChangeEvent to fire twice. try below way
if (value == _token) return;
public string Token
{
get => _token;
set
{
if (value == _token)
{
return;
}
_token = value;
OnPropertyChanged();
}
}

Related

How to use LostFocus in Caliburn Micro

I need to validate the value in the text box when the users finish their input.
When they go to the next text box, the previous text box will validate the range of input (100~100000 steps 100 and format is "###,###"). And my opinion is Lost Focus Event is the good way to solve this.
I have this property which I want to use Lost Focus
private string resultCommandNote02;
public string ResultCommandNote02
{
get
{
return resultCommandNote02;
}
set
{
resultCommandNote02 = value;
NotifyOfPropertyChange(() => ResultCommandNote02);
}
}
And here is the code of View.XAML
<TextBox Grid.Column="1" Margin="0 0 0 4" Text="{Binding ResultCommandNote01}"
cal:Message.Attach="[Event LostFocus] = [Action TxtCmdNote01_LostFocus]"></TextBox>

Binding property only gets updated in code but not in the UI

Background: In my View I have a TextBlock and a TextBox. As soon as the text changes in the TextBox the TextChanged event gets fired and after filtering a list, I want to update the property which is bound to the TextBlock.
In my case it's a counter that shows the number of contacts in the current list.
Problem: When I debug the property (ContactsCount) gets always updated correctly, but only in Code and not in the UI. Strangely enough the UI only updates after I delete the text from the TextBox, to the last list count, but not the actual one.
Code
View:
<TextBlock Text="{Binding ContactsCount, UpdateSourceTrigger=PropertyChanged}"
d:Text="4 Contacts"/>
<xctk:WatermarkTextBox Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Watermark="Search Contact"
Margin="20,10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding Path=SearchBoxTextChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</xctk:WatermarkTextBox>
ViewModel:
public string ContactsCount
{
get => contactsCount;
set
{
contactsCount = value;
OnPropertyChanged(ContactsCount);
}
}
public string SearchText
{
get => searchText;
set
{
searchText = value;
OnPropertyChanged(SearchText);
}
}
public CommandHandler SearchBoxTextChanged { get; set; }
SearchBoxTextChanged = new CommandHandler(TextChanged);
private void TextChanged()
{
var filteredList = contactsList.Where(c => c.FirstName != null && c.FirstName.Contains(searchText.ToLower(), StringComparison.OrdinalIgnoreCase) ||
c.SecondName != null && c.SecondName.Contains(searchText.ToLower(), StringComparison.OrdinalIgnoreCase));
Contacts = new ObservableCollection<Contact>(filteredList);
// Bug: Doesn't update the UI after ContactsCount gets changed
ContactsCount = $"{Contacts.Count} Contacts";
}
You didn't post the code of your OnPropertyChanged() method, but I suspect if should be
OnPropertyChanged("SearchText");
or better
OnPropertyChanged(nameof(SearchText));
i.e. pass in the name of the updated property, not its value.

WPF - Strange behaviour of TextBox when disabling from view model

I have this xaml code in user control:
<StackPanel Orientation="Horizontal" IsEnabled="{Binding CanEditOpenCut}">
<TextBox Text="{Binding OpenCut}" Margin="5" Width="80"/>
<TextBlock Text="°C" />
</StackPanel>
And I have viewmodel with properties:
private decimal _openCut;
public decimal OpenCut
{
get
{
return _openCut;
}
set
{
if (Set(ref _openCut, value))
{
RaisePropertyChanged(() => OpenCut);
if (this.PrevRunConfig != null)
{
this.PrevRunConfig.CloseCut = _openCut;
}
}
}
}
private bool _canEditOpenCut;
public bool CanEditOpenCut
{
get
{
return _canEditOpenCut;
}
set
{
if (Set(ref _canEditOpenCut, value))
{
}
}
}
Now the binding works fine when I set in textbox e.g. 40 then I have it in my OpenCut property in viewmodel. If I set something in viewmodel I get it in the textbox. So far its working. Now I try to disable the textbox and reset the value to some default like 20. There are 2 scenarios one is working and one not.
1) If I set e.g. 40 in textbox, then lose focus and then set CanEditOpenCut = false and OpenCut = 20, everything works I see 20 in textbox.
2) I set 40 to the textbox, I DO NOT !!! lose focus of the textbox, then I set CanEditOpenCut = false and OpenCut = 20. Now it is not working I still see 40 in textbox although the values in viewmodel are correct.
Thanks for any help,
Tomas

Two-way-binding: editing passed value from XAML control in the model setter does not update control

This is for a Windows 10 Universal App.
XAML:
<RelativePanel Padding="4" Margin="4,12,0,0">
<TextBlock x:Name="Label" Text="Class Name" Margin="12,0,0,4"/>
<ListView x:Name="ClassTextBoxes"
ItemsSource="{Binding TextBoxList}"
SelectionMode="None" RelativePanel.Below="Label">
<ListView.ItemTemplate>
<DataTemplate >
<RelativePanel>
<TextBox x:Name="tbox"
PlaceholderText="{Binding PlaceHolder}"
Text="{Binding BoxText,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Padding="4" Width="200" MaxLength="25"/>
<TextBlock x:Name="errorLabel"
RelativePanel.Below="tbox"
Text="{Binding Error, Mode=TwoWay}"
Padding="0,0,0,4"
FontSize="10"
Foreground="Red"/>
<Button Content="Delete" Margin="12,0,0,0" RelativePanel.RightOf="tbox"/>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RelativePanel>
Model:
public class TextBoxStrings : BaseModel
{
private string _placeholder;
public string PlaceHolder
{
get { return _placeholder; }
set
{
if (_placeholder != value)
{
_placeholder = value;
NotifyPropertyChanged();
}
}
}
private string _boxText;
public string BoxText
{
get { return _boxText; }
set
{
if (_boxText != value)
{
_boxText = CheckBoxText(value);
NotifyPropertyChanged();
}
}
}
public string CheckBoxText(string val)
{
var r = new Regex("[^a-zA-Z0-9]+");
return r.Replace(val, "");
}
}
ViewModel:
private TrulyObservableCollection<TextBoxStrings> _textBoxList;
public TrulyObservableCollection<TextBoxStrings> TextBoxList
{
get { return _textBoxList; }
set
{
if (_textBoxList != value)
{
_textBoxList = value;
RaisePropertyChanged();
}
}
}
and I add new TextBoxString objects to my TextBoxList collection from within my view-model.
I want to make it that users can't type in certain characters (or rather, they get deleted whenever they
are typed in.
This works...in the model. Setting breakpoints and looking at the values, everything in the Model is working: value goes into the setter and gets changed, _boxText holds the new value that is set from CheckBoxText();
But the problem is, in my View, the textbox doesn't reflect changes to the underlying text that I make in the model.
So if I type in "abc*()" into "tbox", the value in the model will be "abc". The value of the textbox, however, will still be "abc*()".
I have a feeling it has something to do with the fact that I'm editing items that are inside of a collection and I don't have anything implemented to handle changing items within a collection. I was under the impression that using INotifyPropertyChanged and ObservableCollection<T> would take care of that for me.
Does anyone have any suggestions?
Thank you!
Edit: So, now I'm trying to use TrulyObservableCollection because I thought this was the problem, but it hasn't helped. Here it is: https://gist.github.com/itajaja/7507120
But the problem is, in my View, the textbox doesn't reflect changes to the underlying text that I make in the model.
As you've seen, the TextBox do reflect changes to your model. When you type in "abc*()" in the TextBox, the value in the model will be changed to "abc". The problem here is that the binding system in UWP is "intelligent". For TwoWay bindings, changes to the target will automatically propagate to the source and in this scenario, binding system assumes that the PropertyChanged event will fire for corresponding property in source and it ignores these events. So even you have RaisePropertyChanged or NotifyPropertyChanged in you source, the TextBox still won't update.
In WPF, we can call BindingExpression.UpdateTarget Method to force the update. But this method is not available in UWP.
As a workaround, you should be able to use TextBox.TextChanged event to check the input like following:
private void tbox_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = sender as TextBox;
if (tb != null)
{
var originalText = tb.Text;
var r = new Regex("[^a-zA-Z0-9]+");
if (originalText != r.Replace(originalText, ""))
{
var index = (tb.SelectionStart - 1) < 0 ? 0 : (tb.SelectionStart - 1);
tb.Text = r.Replace(originalText, "");
tb.SelectionStart = index;
}
}
}
However it may break your MVVM model, you can use data validation to avoid this and here is a blog: Let’s Code! Handling validation in your Windows Store app (WinRT-XAML) you can refer to. And for my personal opinion, data validation is a better direction for this scenario.
if (_boxText != value)
{
_boxText = CheckBoxText(value);
NotifyPropertyChanged();
}
Try changing this to:
var tmp = CheckBoxText(value);
if (_boxText != tmp)
{
_boxText = tmp;
NotifyPropertyChanged();
}
I hope, in your XAML, the binding to property BoxText is two-way, right?
You should edit BoxText and then send checked value to UI. Just send value to CheckBoxText and already edited should be assigned to _boxText. And then you should send BoxText to UI by calling RaisePropertyChanged("BoxTest"). Please, see the following code snippet:
private string _boxText;
public string BoxText
{
get { return _boxText; }
set
{
if (_boxText != value)
{
_boxText=CheckBoxText(value);
RaisePropertyChanged("BoxText");
}
}
}
There is no difference where you use INotifyPropertyChanged for one property of for properties placed in collection. The complete example with collections and ListView can be seen here

How to clear textbox on click button [duplicate]

This question already has answers here:
How to clear text box on click in MVVM
(2 answers)
Closed 7 years ago.
I have a textbox in which I am handling its text changed event.Now when I click button I want to clear the text from the textbox.
Now when I have text in the textbox and when I call my command the text is not cleared.
xaml
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="mytxtBox">
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=SearchCommand}" CommandParameter="{Binding ElementName=mytxtBox, Path=Text}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
ViewModel
public string SearchText
{
get
{
return TypedText;
}
set
{
TypedText=value;
if (string.IsNullOrEmpty(TypedText.ToString()))// This is called when the text is empty
{
Some Logic//
}
SetProperty(ref TypedText, value);
}
}
private void MyCommandExecuted(string text)
{
SearchText= string.Empty;
}
You seem not to understand the framework you are using
public string SearchText
{
set
{
TypedText = value;
SetProperty(ref TypedText, value);
}
}
These two lines of code should/could NEVER ever be in the same block of code EVER.
What is happening is this.
The first line sets TypedText to value. OKAY...
Second line, check if TypedText is equal to value (spoiler alert, it is), and set them to be equal if not AND THEN TELL WPF that you changed to value.
The problem is, the second line never runs its logic (of tell WPF that I've changed). The reason this never runs is the first line.
Remove TypedText = value; from your code and it might just work.
set
{
if (string.IsNullOrEmpty(value))// This is called when the text is empty
{
Some Logic//
}
SetProperty(ref TypedText, value);
}
However, one last thing. I really really really hate code where the setter DOES stuff. Why is there logic here? From an external user, it might do something unexpected.
I have a textbox in which I am handling its text changed event
No you don't, or at least not in the code excerpt that you have shown in your question:
<TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Name="mytxtBox">
<TextBox.InputBindings>
<KeyBinding Command="{Binding Path=SearchCommand}" CommandParameter="{Binding ElementName=mytxtBox, Path=Text}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
In this example, you have a string property data bound to the TextBox.Text property, which is similar, but not the same as handling its text changed event.
Either way, in order to clear this data bound value, you just need to set your data bound string property to an empty string (after removing that extraneous code from the setter):
public string SearchText
{
get { return TypedText; }
set { TypedText = value; SetProperty(ref TypedText, value); }
}
...
private void MyCommandExecuted(string text)
{
SearchText = string.Empty;
}

Categories

Resources