This is my Button declaration, written in .xaml file:
<dxlc:LayoutGroup Orientation="Horizontal" Margin="5,15,0,5">
<Grid MinWidth="100">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button
IsEnabled="{Binding IsSearchCriteriaHasValue}"
Content="Search"
MaxHeight="25"
MaxWidth="70"
ClipToBounds="True"
VerticalAlignment="Center"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding SearchCommand}"/>
</Grid>
</dxlc:LayoutGroup>
This is the function that returns true/false whether the user has typed any search text in the search box next to the search button. The function is in another .cs file:
public bool isButtonEnabled
{
return (SearchBox.Selection.Count > 0);
}
The problem is that the value of isEnabled never changes, it stays true, i.e. the button stays enabled all the time or if I change the > sign, the button stays disabled all the time. Any suggestions?
The IsSearchCriteriaHasValue
needs to raise an event that it changed, you can do that by using INotifyPropertyChanged
interface:
public class Customer : INotifyPropertyChanged
{
// INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
// Your property
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
}
You can write trigger for button (SearchBox looks a UI control)
<Style TargetType="Button"> <Setter Property="IsEnabled" Value="True" /> <Style.Triggers> <DataTrigger Binding="{Binding Selection.Count, ElementName=SearchBox}" Value="0"> !-- You can use convertor also <Setter Property="IsEnabled" Value="False" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button>
Its not problem with XAML, its problem with notification. I hope you are aware of INotifyProperty interface's NotifyPropertyChanged Event. Only if that is triggered for a property in ViewModel, then the UI will get the notification and updates it.
If SearchBox.Selection count changes, it doesnt trigger any notification for IsSearchCriteriaHasValue property that it has been changed and the UI should update.
Lets say SearchBox.Selection is bound to a observableCollection and the collection changes, then subscribed to the observableCollection for add/remove then trigger NotifyPropertyChanged("IsSearchCriteriaHasValue")
; there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With