Event when clicking on a date in a calenderview in UWP - c#

I would like to add a click event, which sends information about the day, on the dayitem in the CalenderView in UWP. I'm having a hard time figuring out how.
[
I've already tried following, but it does not work and I can imagine that there's better ways to do it.
private async void KalenderView_DoubleTappedAsync(object sender, DoubleTappedRoutedEventArgs e)
{
Viewmodel.Calendar_ViewModel calender_Viewmodel = new Viewmodel.Calendar_ViewModel();
if ( KalenderView.SelectedDates != null )
{
await calender_Viewmodel.OpenNewWindowAsync();
}
else
{
}
}
}
and the XML:
<Grid>
<CalendarView
x:Name="KalenderView"
DoubleTapped="KalenderView_DoubleTappedAsync"
CalendarViewDayItemChanging="CalendarView_CalendarViewDayItemChanging" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" Height="526" Width="936"
SelectionMode="Single"
DisplayMode="Month"/>
</Grid>

you can use SelectedDatesChanged event for this purpose it will be invoked whenever you change the date from the calender in any way and within this event you can see the new date of the calender which is currently selected and access, just the dayitem or any other properties on the calenderdayitem.
if you want to specifically add an event to when a day item can be clicked it can be a bit complicated, as you will have to create a control ( refer : Templated Control vs Custom Control in UWP. No clear answer found online ) but I suggest dont do that unless you have no other option and you know what you are doing.

Related

Add Control to User Control

I created a user control with WPF which is supposed to take in a list of controls as its content.
I succeeded in creating such a user control, but have run into the issue that I cannot name any of the sub-controls I added.
Here's the code for the user control:
public static DependencyProperty InnerContentProperty = DependencyProperty.Register("TaskListItems",
typeof(UIElement), typeof(TaskList));
public UIElement TaskListItems {
get { return (UIElement)GetValue(InnerContentProperty); }
set { SetValue(InnerContentProperty, value); }
}
And the XAML (relevant part):
<ScrollViewer>
<ContentControl Content="{Binding TaskListItems, RelativeSource={RelativeSource AncestorType={x:Type local:TaskList}}}"/>
</ScrollViewer>
The implementation then looks like so:
<uc:TaskList Grid.Column="0" HeaderText="Daily Task List" FooterText="Completed: 10">
<uc:TaskList.TaskListItems>
<StackPanel>
<Button Content="Some Button"/>
<Button Content="Some Button 2"/>
</StackPanel>
</uc:TaskList.TaskListItems>
</uc:TaskList>
So when I add a control to my user control such as a button for example, everything works fine, except I cannot give the button a name and I get the error:
Cannot set Name attribute value 'test' on element 'Button'. 'Button' is under the scope of element 'TaskList', which already had a name registered when it was defined in another scope
So my question is, is there a way to functionally do the same thing here, but also letting me give my controls names?
Edit for more information:
What I'm going for is a user control that can take in a list of other user controls I created.
Those "list item" controls have four buttons each (the text bit is also a button):
The text button just opens a window with info about the selected task.
The check mark button is supposed to create an event to request the tasks completion.
The vertical ellipsis button opens a window where you can modify the task.
Lastly the x button is supposed to create an event to request cancellation/removal of the selected task.
Issue lies with creating the events and subscribing to them. What I have so far are two event handlers which are invoked in each of the buttons click events.
public event EventHandler<TaskItemEventArgs> TaskCompletedEvent;
public event EventHandler<TaskItemEventArgs> TaskRemoveRequestEvent;
private void acceptBtn_click(object sender, RoutedEventArgs e)
{
TaskCompletedEvent?.Invoke(this, new TaskItemEventArgs() { EventText = "Task Completed!" });
}
private void removeBtn_click(object sender, RoutedEventArgs e)
{
TaskRemoveRequestEvent?.Invoke(this, new TaskItemEventArgs() { EventText = "Task Remove Requested!" });
}
The problem now is that I don't know where to subscribe these events to in order to modify the "task item" control that invoked the event. I was thinking I should simply add it in the parent control, but I wouldn't know how to go about that, as all the controls here are dynamic.
Now that I think about it, I guess you wouldn't need a control name for the above task. The dynamic controls have me confused though.
I actually got the code to work now after a bit of experimentation and googling. I dynamically create a stack panel in the TaskList control and when I hit the "plus" button in the top right corner, a dialog opens allowing me to describe my task. After that I dynamically create a TaskListItem and subscribe to it's events with some methods I created in TaskList. Finally I add the TaskListItem to the StackPanel and if I want to modify it I can just modify the specifc object that asked to be edited. Works pretty well.

Using two different events to update a ViewModel property in WPF

I apologize if the question title isn't really specific, I'm not exactly sure how to condense the problem I'm having down to a few words. But to simplifiy the problem I'm having, here is my issue:
I'm creating a tool using WPF that consists of a TextBox that will contain a path to a directory and a Button that will allow you to Browse to a certain directory. Now, when I select the Browse button, it pops up a dialog, allows the user to select a directory and then I have some methods that will disable some buttons and updates some Brushes on the screen if the path doesn't meet a certain set of criteria. No problems there, got that working.
My problem is the TextBox that this Browse button correlates with. This TextBox is using a binding as such:
In my MainWindow.xaml (Yes, this is the simplified, focused version):
<Window>
<TextBox Text="{Binding Directory}" TextChanged="Directory_TextChanged" />
<Button Content="Browse..." Click="Browse_Click"/>
</Window>
In my code MainWindow.xaml.cs file:
public partial class MainWindow: Window
{
private ViewModel myViewModel;
public MainWindow()
{
myViewModel = new ViewModel();
this.DataContext = myViewModel;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Dialog stuff that's working
viewModel.Directory = dialog.SelectedPath;
}
private void InstallDir_TextChanged(object sender, TextChangedEventArgs e)
{
ValidatePath(); /* Disables/enables buttons and updates brushes based on validation. Also working */
}
private void ValidatePath() {/* */}
}
Like I mentioned earlier, the browse button works fine. I'm trying to figure out however, how I can get this to work if I type a directory alongside it. Because if I type something in the textbox, that would mean that inside of the InstallDir_TextChanged() function I would have to set viewModel.Directory, but since I have the INotifyPropertyChanged attached to this ViewModel, this function would get called recursively.
I tried doing the validation stuff within the viewmodel, but I couldn't figure out how to update the brushes/buttons in MainWindow if I did this. (Still relatively new to C# so I haven't learned the ins and outs yet. This is the first WPF tool I've been making from scratch, so just a disclaimer).
Would anyone have any ideas (or logic) I can approach to try and accomplish this? If there's any further clarification needed, that's not an issue. I don't need an exact definitive answer. Maybe some advice that could point me in the correct direction would definitely suffice. I don't have a problem trying to figure stuff out.

WPF Extended ToolKit - Selecting value in DecimalUpDown

After hours of trying to solve this, I'm giving up and am actually wondering if there's a solution for this.
I have a WPF View that is setting the focus on a decimal up down from the code behind (xaml.cs). My aim is to select the value inside it since it loads "0.00" and it's frustrating for the user to have to delete it before he enters his value. I tried the following code in the loaded method:
private void Loaded_Window(object sender, EventArgs e)
{
txtAmount.AutoSelectBehavior = AutoSelectBehavior.OnFocus;
txtAmount.Focus();
Keyboard.Focus(txtAmount);
}
I have also tried to set SelectAllOnGotFocus as true and it still did not work. Funnily enough, this selects the test if I put a debug point in the method so I'm thinking that it has to do with the loading of the User Control. Does anyone have any pointers of how to make this work?
The xaml decimalupdown:
<xctk:DecimalUpDown x:Name="txtAmount" d:LayoutOverrides="Height" Minimum="0" Maximum="99999999.99" ShowButtonSpinner="False" UpdateValueOnEnterKey="True" Text="{Binding Amount, UpdateSourceTrigger=PropertyChanged}" AllowSpin="False" FormatString="N2" >
Try the following:
private void [NAME OF YOUR EVENT GOES HERE] (object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
{
SelectAll();
}
}
The "SelectAll" is really the main thing here, you can ignore the if statement i believe.
In the XAML you will have to add the property "GotFocus" to your control like so:
<xctk:DecimalUpDown GotFocus="[NAME OF YOUR EVENT GOES HERE]" />
So that whenever it gains focus, the first thing it does is select everything ready for editing.

Sharpdx tookit input won't work with windows 10 universal apps

I'm trying out sharpdx tookit with universal app in windows 10 and can't get the input to work. I know I have to create different input for different devices but for now I just want to try keyboard.
So I follow this link first to set up the project: Is there a SharpDx Template, for a Windows Universal App?
And right now I'm trying input like I usually do it:
In the constuctor:
_keyboardManager = new KeyboardManager(this);
In the update method:
var state = _keyboardManager.GetState();
if (state.IsKeyDown(Keys.B))
{
//do stuff
}
But he never register that the key B is down (or any other key I have tried). I have also tried GetDownKeys() but the list is always empty.
So does any one have any idea what to do here?
And there's an another, better, easier version:
using Windows.UI.Core;
using SharpDX.Toolkit;
using Windows.System;
namespace Example
{
class MyGame : Game
{
public MyGame()
{
CoreWindow.GetForCurrentThread().KeyDown += MyGame_KeyDown;
}
void MyGame_KeyDown(CoreWindow sender, KeyEventArgs args)
{
System.Diagnostics.Debug.WriteLine(args.VirtualKey);
}
}
}
You have to subscribe to the CoreWindow's event, that's it :)
HEUREKA!
I had the same problem, but with windows 8.1. It took me 2 days to find a solution. It's the foolish SwapChainPanel, which can't be in focus because the class doesn't inherit from Control class, so it won't handle keyboard events.
The solution is HERE that is to say, you have to put an XAML element that's inherited from the Control class, like Button to handle the event. My XAML file is like this:
<SwapChainPanel x:Name="_swapChainPanel"
Loaded="_swapChainPanel_Loaded"
KeyDown="_swapChainPanel_KeyDown">
<Button x:Name="_swapChainButton"
Content="Button"
HorizontalAlignment="Left"
Height="0"
VerticalAlignment="Top"
Width="0"
KeyDown="_swapChainButton_KeyDown">
</Button>
</SwapChainPanel>
In the XAML.cs I handled the events this way:
private void _swapChainButton_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
e.Handled = false; //This will pass the event to its parent, which is the _swapChainPanel
}
private void _swapChainPanel_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
game.KeyboardEvent();
}
In the KeyboardEvent() method I put the if things... and you have to make the button in focus manually, with code. "_swapChainButton.Focus(FocusState.Programmatic);"
But last, it's not so good for me, it's so slow. It has delay. :/

What could be preventing my combobox dropdown from showing after app focus is lost?

I have a ComboBox in a WPF app that has recently been refactored to use the MVVM pattern. An apparent side effect to this change is that changing focus to another application while the combobox dropdown is visible completely prevents the dropdown from being visible again, until the app has been restarted.
The ComboBox DataContext is set to my ViewModel, with its ItemsSource bound to an ObservableCollection<String> SearchSuggestions, and IsDropdownOpen bound to a property SuggestionsVisible in the ViewModel.
The desired effect is a search box with autocomplete suggestions. It should close if there are no suggestions in the ObservableCollection, if the user cancels the search, if the user runs the search, or if the user clicks away from the text field - either inside the app or outside it.
The ViewModel explicitly sets the SuggestionsVisible property to true or false based on whether SearchSuggesions contains any items after user input. This process continues to take place after this bug manifests itself, just with no visible change to the UI. Any idea why losing focus while the dropdown is open renders the dropdown un-openable for the rest of the app's session?
Here's how I have things wired together:
<ComboBox DataContext="{Binding SearchBoxVm}" Name="cmboSearchField" Height="0.667"
VerticalAlignment="Top" IsEditable="True" StaysOpenOnEdit="True"
PreviewKeyUp="cmboSearchField_OnKeyUp"
PreviewMouseLeftButtonUp="cmboSearchField_OnPreviewMouseLeftButtonUp"
Background="White" ItemsSource="{Binding SearchTopics}"
IsDropDownOpen="{Binding SuggestionsVisible,
UpdateSourceTrigger=PropertyChanged}"
Margin="50.997,15.333,120.44,0"
RenderTransformOrigin="0.5,0.5" Grid.Row="1" >
<!-- SNIP STYLING -->
</ComboBox>
ViewModel:
public class SearchBoxViewModel : INotifyPropertyChanged
{
public void ResetSearchField(bool preserveContents = false)
{
if (!preserveContents || string.IsNullOrEmpty(Query))
{
Foreground = Brushes.Gray;
QueryFont = FontStyles.Italic;
Query = DEFAULT_TEXT;
}
}
public bool OnKeyUp(Key key)
{
bool showDropdown = SuggestionsVisible;
bool changeFocusToCombobox = false;
if (keyInValidRange(key))
{
SearchSuggestions = GetSearchSuggestions(Query);
if (SearchSuggestions.Count > 0)
{
SuggestionsVisible = true;
}
}
return changeFocusToCombobox;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
bool _suggestionsVisible = false;
public bool SuggestionsVisible
{
get { return _suggestionsVisible; }
set
{
// this section is still called after this issue manifests,
// but no visible change to the UI state is made
_suggestionsVisible = value;
NotifyPropertyChanged("SuggestionsVisible");
}
}
public ObservableCollection<String> SearchTopics = new ObservableCollection<String>();
}
The OnKeyUp() method is called by the MainWindow class ( haven't gotten as far as binding events to handlers specified in the ViewModel ), while but there's also a call to ResetSearechField from the MainWindow:
// Note: removing references to this event handler does not have any effect
// on the issue at hand... only including here for completeness
void window_Deactivated(object sender, EventArgs e)
{
SearchBoxVm.SuggestionsVisible = false;
SearchBoxVm.ResetSearchField(true);
}
I've spent quite a bit of time trying to debug this, and haven't seen any internal state changes that might account for this. The NotifyPropertyChanged event is otherwise behaving as it did before, and the stack trace window isn't showing any exceptions having been encountered.
Setting the binding mode on the IsDropdownOpen property to 'TwoWay' in the XAML hasn't had any effect either. Lastly, wrapping the assignment to SuggestionsVisible in a Dispatcher call on the main thread has had no effect on the issue either.
Any assistance would be appreciated.
#BrMcMullin, since you have stated that:
The desired effect is a search box with autocomplete suggestions.
may I ask, why do you choose to use standard ComboBox instead of specialized AutoCompleteBox that is available in the WPF Toolkit - February 2010 Release and seems like was especially designed for your case?
You may have noticed that first link points to documentation for its Silverlight predecessor, but don't worry - WPF Toolkit library include fully functional official WPF port of AutoCompleteBox from Silverlight. There is more info about this "event": AutoCompleteBox: Now with 100% more WPF.
With that control your auto complete popup could looks as simple as:
or as complex as:
So, if you will not manage to solve your issue with ComboBox's popup visibility, feel free to give a try to AutoCompleteBox. With it you could even leverage dynamic sorting of your suggestions if needed (just use answer from #adabyron).

Categories

Resources