I'm working on a wpf application and in one of the views I have a GroupBox that has som TextBoxes, CheckBoxes ..etc and a search button. under that GroupBox I have a TabControl that has 2 TabItems. Each TabItem shows a different DatGrid that are connected to different databases and the GroupBox above is used to execute a search command from a TextBox and to do filtering using the rest of the elements. One of the databases is huge so I need to prevent the user from executing a search if with less than 2 letters but the user should be able to call everything in the other database. I implemented IDataErrorInfo in my view model and my CanSearchCommandExecute method looks like this:
Public Overrides Function CanSearchCommandExecute(obj As Object) As Boolean
Dim vm As PersonSearchViewModel = TryCast(obj, PersonSearchViewModel)
If vm IsNot Nothing Then
If vm.PersonView Is CollectionViewSource.GetDefaultView(vm.PersonDataGridList) Then
Return True
End If
End If
If vm IsNot Nothing Then
If vm.IPIDataGrid IsNot Nothing Then
If Item("Enamn") IsNot Nothing Then
Return False
End If
End If
End If
Return PersonDataGridList IsNot Nothing
End Function
and in my Xaml file the search button looks like this:
<Button Command="{Binding SearchCommand}" IsDefault="{Binding ElementName=txtEnamn , Path=IsKeyboardFocused}" CommandParameter="{Binding}" Content="_Sök" Grid.Column="8"
Height="23" Width="100" HorizontalAlignment="Left"
x:Name="btnSearch" ToolTip="Klicka eller tryck på enter för att söka." />
and the TextBox that is bound to the search command looks like this:
<TextBox Grid.Column="1" AutomationProperties.AutomationId="Enamn" x:Name="txtEnamn" ToolTip="Sök efternamn. Söker även i fälten förnamn, pseudonym och viktigt info."
>
<TextBox.Text >
<Binding Path="Enamn" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
The expected result is:
when I open the window the search button is enabled, and the textbox border is the blue-ish default color
when I toggle to the second tab item the search button is disabled and the textbox border is red until the user types at least 2 letters.
when I toggle back to the first tab item the search button should be enabled and the textbox border sholud go back to the default color.
the actual result is when I open the window the search button is enabled, and the textbox border is the blue-ish default color as excepted
when I toggle to the second tab item the search button is disabled as expected but the text box border is the default color and not red, and when I start typing it becomes red until i type 2 letters. and if I delete what I typed it stays red as expected,
now if it's red and I toggle back to the first tab item the search button gets enabled again as expected but the textbox border continues to be red until I start typing. and so on.
The CanSearchCommandExecute is Raised correctly cause the search button is responding as it should but what's confusing me it the TextBox border, it's not changing as it should.
feel free to answer in c# I'm working on a very old project that uses VB in the UI.
It seems that i had to invoke OnPropertyChanged on the property bound to the text box when changing the Collection view like this
Public Property PersonView As ICollectionView
Get
Return _personView
End Get
Set(value As ICollectionView)
_personView = value
OnPropertyChanged("Enamn")
End Set
End Property
and that fixed my problem