Calendar MouseLeftButtonDown is fired after clicking outside of the component - c#

I want to raise event using MouseLeftButtonDown by clicking on a date in Calendar Control. But it is not raised until I click outside of this component.
Here is the xaml code:
<Controls:BasicCalendar Grid.Row="0"
x:Name="DemoCalendar"
DisplayDate="{Binding Path=DisplayDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HighlightedDateText="{Binding HighlightedDateText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,0,0,0"
DateHighlightBrush="Gold">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cal:ActionMessage MethodName="CalendarSelectDateEvent">
<cal:Parameter Value="{Binding ElementName=DemoCalendar, Path=SelectedDate}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Controls:BasicCalendar>
Here is the event which I want to raise after clicking on some date in calendar:
public void CalendarSelectDateEvent(DateTime selectedDate)
{
this.ActualCalendarEvents = this.CalendarEvents.Where(x => x.Date >= selectedDate).ToList();
var a = this.ActualCalendarEvents;
this.ActualCalendarEvents = null;
NotifyOfPropertyChange(() => ActualCalendarEvents);
this.ActualCalendarEvents = a;
NotifyOfPropertyChange(() => ActualCalendarEvents);
}
When I tried to use Click event instead of MouseLeftButtonDown the Calendar Control was automatically handling it without firing CalendarSelectDateEvent. Is there a way to fire both events? (For Calendar Control selecting and highlighting the dat and for me CalendarSelectDateEvent)

I already figured it out. It was really easy in the end. The Calendar Control has property SelectedDate. I just binded SelectedDate to my property SelectedDateChanged in code behind and when the SelectedDate changed I raise the event CalendarSelectDateEvent.
<Controls:BasicCalendar Grid.Row="0"
x:Name="DemoCalendar"
DisplayDate="{Binding Path=DisplayDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HighlightedDateText="{Binding HighlightedDateText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,0,0,0"
SelectedDate="{Binding SelectedDateChanged}"
DateHighlightBrush="Gold">
</Controls:BasicCalendar>
Like this:
public DateTime SelectedDateChanged
{
get
{
return selectedDateChanged;
}
set
{
this.selectedDateChanged = value;
this.CalendarSelectDateEvent(this.selectedDateChanged);
}
}

Related

C# WPF SelectionChangedEvent - reset selection - better options?

I have a datagridview populated with items and I am using a SelectionChanged event to populate textboxes from that data when selected.
If I make a selection, everything works. If I click elsewhere in the App and then come back to click the SelectionChanged event again on the same item - it doesn't work.
According to MSDN:
"This event occurs whenever there is a change to a selection."
MSDN SelectionChangedEvent
So it appears that despite clicking elsewhere, resetting the Textboxes - the selected item is not changing as the SelectionChanged event no longer triggers - click on another item and it works, click back again and it works - but click on it, reset textboxes, click it again - nothing happens, this includes clicking in the datagridview itself in a blank area.
XAML:
<DataGrid x:Name="TimeView" Grid.Row="1" Grid.Column="3"
Grid.ColumnSpan="3" Grid.RowSpan="4" Margin="10 50 10 10"
CanUserAddRows="False" Visibility="{Binding StartTiming}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cal:ActionMessage MethodName="SelectedTimeChangeEvent">
<cal:Parameter Value="$eventArgs" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
ViewModel
public void SelectedTimeChangeEvent(SelectionChangedEventArgs e)
{
foreach (TimeData addedRow in e.AddedItems)
{
TbID = addedRow.ID;
TbDate = addedRow.Date;
TbStartTime = addedRow.StartTime;
TbDescription = addedRow.Description;
}
}
Since I am using MVVM and Caliburn, TimeView is connected to an ICollection, which is in turn connected to an ObservableCollection:
private ObservableCollection<TimeData>? _timeCollection;
public ObservableCollection<TimeData>? TimeCollection
{
get { return _timeCollection; }
set
{
_timeCollection = value;
NotifyOfPropertyChange(() => TimeCollection);
}
}
private ICollectionView? _timeView;
public ICollectionView? TimeView
{
get { return _timeView; }
set
{
_timeView = value;
NotifyOfPropertyChange(() => TimeView);
}
}
There is a work around, which is the following after populating the Textboxes:
TimeView = null;
TimeView = CollectionViewSource.GetDefaultView(TimeCollection);
This works, but I thought that there might be a "deselect" option that would be better than repopulating every time a selection is made, one of my Datagrids contains 15,000 items, and it is still instant, but seems overkill to populate it every time a selection is made.
i would recommend bindings, they automaticly reset when nothing is selected
<DockPanel>
<StackPanel DataContext="{Binding SelectedTime}" DockPanel.Dock="Left">
<TextBlock Text="{Binding ID}"/>
<TextBlock Text="{Binding Date}"/>
<TextBlock Text="{Binding StartTime}"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
<DataGrid ItemsSource="{Binding TimeView}" SelectedItem="{Binding SelectedTime}">
...
</DataGrid>
</DockPanel>
public TimeData SelectedTime
{
get { return _selectedTime; }
set
{
_selectedTime = value;
NotifyOfPropertyChange(() => SelectedTime);
}
}
also there is this neet feature
protected virtual void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
field = value;
OnPropertyChanged(propertyName);
}
so you can write
set { SetValue(ref _selectedTime, value) }

How to apply Search in Editable Combobox in WPF using MVVM

I have editable combobox in WPF. which is having list of order number.
I need to implement below scenario in my code.
The user can fill the starting of order number, and, the system propose the close order number available in the dropdown list.
Can anyone suggest how to do that?
In My Viewmodel i have written:
public void _fillREOrderNumbers()
{
List<FinishedReprintingOrderNumber> orders = _finishedProductReprintService.GetFinishedProductReprintbyOrder().ToList();
foreach (var item in orders)
{
ReOrders.Add(item);
}
}
This is loading the order number in drop down.
View or XAML:
<ComboBox x:Name="cbOFab" HorizontalAlignment="Left" Margin="373,81,0,0"
VerticalAlignment="Top" Width="262" IsEditable="True"
ItemsSource="{Binding ReOrders, Mode=TwoWay}" DisplayMemberPath="codOrder" SelectedItem="{Binding
ReSelectedOrder}" Background="{DynamicResource dgridRowColor}" />
Till Now,
I am able to populate the order number in my combo box but I am not aware how to search inside it.
I have implemented filtering of items in ComboBox this way.
Here is XAML:
<ComboBox
MinWidth="200"
ItemsSource="{Binding Path=Shops.View, RelativeSource={RelativeSource TemplatedParent}}"
DisplayMemberPath="NameExtended"
SelectedItem="{Binding Path=SelectedShop, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
IsTextSearchEnabled="False"
IsEditable="True"
IsDropDownOpen="{Binding Path=ComboOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
StaysOpenOnEdit="True"
Text="{Binding Path=SearchText, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction
Command="{Binding Path=FilterCommand, RelativeSource={RelativeSource TemplatedParent}}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
You need all lines from IsTextSearchEnabled and below.
When you press any key in combo box, it opens in up and filters items in it, using property SearchText bound to ComboBox.Text.
Here is the view model code:
public string SearchText { get; set; }
private List<Shop> _shops;
protected void FilterShops()
{
ComboOpen = true;
if (!string.IsNullOrEmpty(SearchText))
{
Shops.UpdateSource(_shops.Where(s => s.NameExtended.ToLower().Contains(SearchText.ToLower())));
}
else
{
Shops.UpdateSource(_shops);
}
OnPropertyChanged("Shops");
}

ComboBox SelectionChanged event firing when we changing value of combobox WPF

Why ComboBox SelectionChanged event fire when we changed value of combobox in WPF?
<telerik:RadComboBox Name="onClockComboBox" Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Width="200" Height="30" StaysOpenOnEdit="True" IsSynchronizedWithCurrentItem="True" OpenDropDownOnFocus="True"
ItemsSource="{Binding TravelOtherModel.OnClockOptionListCollection,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"
DisplayMemberPath="OptionName" SelectedValuePath="RowNo" IsFilteringEnabled="True" IsEditable="True" Margin="80,3,0,0">
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
<ie:InvokeCommandAction Command="{Binding OnClockComboBoxSelectedItemChangedCommand}" CommandParameter="{Binding ElementName=onClockComboBox}" >
</ie:InvokeCommandAction>
</ie:EventTrigger>
</ie:Interaction.Triggers>
</telerik:RadComboBox>
SelectionChanged event Code is -
public void OnClockComboBoxSelectedItemChangedCommand_Event(object obj)
{
try
{
RadComboBox onClockComboBox = obj as RadComboBox;
var item = onClockComboBox.SelectedItem as OnClockOption;
string s= onClockComboBox.SelectedValue.ToString();
onClockComboBox.SelectedValue = "0";
}
catch (Exception ex)
{
}
}
When we set default selected value by below code then its again firing SelectionChanged event why?
onClockComboBox.SelectedValue = "0";
When we set default 'select' value by below code then its again firing
selection Changed event why?
Because it should, otherwise how will you get notified if the combo box is reset. Assuming your selected value is bound to load some other control. It would require to know if the selection has changed even for the default value.

Delay in getting text on text box textchanged event - Windows phone

I created a TextBox. I wrote a TextChanged event in view model. The problem is, when I type 'a' in the TextBox, in the TextChanged event, I am getting "". Then if I type next letter 'b', I am getting the previous result 'a' and If I type the next letter 'c', I am getting 'ab' as result. Why is one char delay in the TextChanged event?
Here is my XAML code:
<toolkit:PhoneTextBox Visibility="{Binding IsSearchBoxVisible,Converter={StaticResource BoolToVisibilityConverter}}" TextChanged="txtboxPhoneContacts_TextChanged" Grid.Row="2" Width="450" FontSize="28" Foreground="Black" x:Name="txtboxPhoneContacts" VerticalContentAlignment="Center" Background="LightGray" Hint="{Binding Localizedresources.SearchContactsText,Source={StaticResource LocalizedStrings}}" Text="{Binding PhoneContactSearchText,Mode=TwoWay,UpdateSourceTrigger=Explicit}" Style="{StaticResource SearchTextBoxStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding PhoneBookTextBoxTextChanged}" CommandParameter="{Binding Text,ElementName=txtboxPhoneContacts,BindsDirectlyToSource=True}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<i:Interaction.Behaviors>
<controls:UpdateOnTextChangedBehavior></controls:UpdateOnTextChangedBehavior>
</i:Interaction.Behaviors>
</toolkit:PhoneTextBox>
My ViewModel.cs
private void OnPhoneBookTextBoxTextChanged(Object obj)
{
if (obj != null)
{
String text = obj.ToString();
searchResults = new ObservableCollection<PhoneBookEntity>(
from item in PhoneContactsList
where item.PhoneContactName.Contains(text)
select item);
}
}
Any suggestions is highly appreciated.

Eventhandler for WPF control in DataTemplate

I've been working with WPF and I have experienced a problem related with DataTemplates.
I have a view called DetailPage.xaml and this view uses a DataTemplate called Detail.xaml. I added a textbox to this DataTemplate and I want to handle the TextChanged event. So I made something like this:
<DataTemplate x:Name="DetailContent">
<Grid Margin="5" DataContext="{Binding Items[0]}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition MaxHeight="80"/>
</Grid.RowDefinitions>
<StackPanel Width="432">
<TextBox Name="NumeroParadaTB" Text="{Binding NumeroParada}" MaxLength="5" TextChanged="NumeroParadaTB_TextChanged" />
</StackPanel>
</Grid>
</DataTemplate>
Then I created and event handler in DetailPage.xaml.cs, like the following:
protected async void NumeroParadaTB_TextChanged(object sender, TextChangedEventArgs e)
{
string nroParada = ((TextBox)sender).Text;
if(!string.IsNullOrEmpty(nroParada) && nroParada.IsDigitsOnly() && nroParada.Length == 5)
{
}
}
But when running, and error is thrown saying that the event handler doesn't exist. I guess I'm using the eventhandler in a wrong way.
Thanks!
Since you're using data binding, I assume, that you have some class with NumeroParada property:
public class SomeClass : INotifyPropertyChanged
{
/* other code here */
public string NumeroParada
{
get { return numeroParada; }
set
{
if (numeroParada != value)
{
numeroParada = value;
OnPropertyChanged("NumeroParada");
}
}
}
private string numeroParada;
}
Setter of this property will fire, when UI will update the binding source. This is your "TextChanged" event.
Note, that by default, TextBox updates Text property, when loosing focus. If you want to perform any action when user changes text, update your binding definition:
Text="{Binding NumeroParada, UpdateSourceTrigger=PropertyChanged}"
So far so good. But this code:
if(!string.IsNullOrEmpty(nroParada) && nroParada.IsDigitsOnly() && nroParada.Length == 5)
suggests, that you're trying to implement validation of value, entered by user.
Validation in WPF is rather big theme, I'd recommend you to read something like this to select validation approach.
Instead of adding an Event handler, you can use Event to Command logic. Create a Command in ViewModel and bind it to the TextChanged event.
<TextBox Text="{Binding SearchText, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Interaction triggers available in System.Windows.Interactivity assembly.

Categories

Resources