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.
Related
So I'm dabbling in C# properly for the first time trying to make a WPF based desktop application.
So far it's mostly going well, however as part of the project I am trying to take input from the user in one window (essentially where they define a project and the settings they want) and save them for later user in the project and have it act accordingly based on their input.
I've figured out the saving of this data for text inputs etc, however I'm having issues replicating this for check boxes.
I've defined the setting in the settings page as a bool defaulting to false, with the intent to be if the user ticks the checkbox then set the setting to true for later use.
When I'm trying to use .Checked against my checkbox class name it says it must appear on the left hand side of of += or -=, I've looked online for clarification and most similar code & relevant tutorials define it the way I have without issue.
Here's a snippet of the code:
public void CXML_GetSettings()
{
CXML_NewProject_Inc_SubModule_XML.Checked = Properties.Settings.Default.CXML_Project_Inc_SubModule;
}
Tried various ways of changing it but just can't get .Checked to work anywhere.
The Checked is an event for CheckBox, if you want to use it for your CheckBox, you can use it like below:
public MainWindow()
{
InitializeComponent();
MyCheckBox.Checked += MyCheckBox_Checked;
}
private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
Or
<CheckBox IsChecked="False" Name="MyCheckBox" Checked="MyCheckBox_Checked"/>
private void MyCheckBox_Checked(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
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.
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.
I have an AutoCompleteBox binded to an ObservableCollection ItemsSource which I filter on my own by querying entities from a domainservice.
I used the scenario of populating from a webservice call from the blog of Jeff Wilcox, by setting the PopulatingEventArgs.Cancel to True, and when my collection is ready, I call PopulateComplete() on the ACB.
My goal is to reopen the dropdown on mouseover (or click) but without reloading again all the data from the web. I found a question on stackoverflow where the answer was to set IsDropDownOpen to True. But in this case, the ACB population starts again, and another call goes to the webservice.
Of course, when the user starts typing, the filtering should be done again.
(for ex. you type "ric" and the box suggests "rice" and "ricin", you select rice, but you change your mind and want to select another one from the same collection, lets say "ricin". In this case you already have the suggestions containing "ric" in memory, no need to load them again..)
I found an alternative way in which instead of setting IsDropDownOpen, I just simply call the PopulateComplete() method. This does exactly the same thing that I want, but with a little fail: after my ACB loses focus, the dropdown is not opened again on mouseover liek it should. Even when I click back into the acb textbox.
So is there a fix for this, or does someone know why the PopulateComplete() only reopens the dropdown when the ACB has focus for the first time? Or this was only my luck that calling this method reopened the dropdown and the IsDropDownOpen property should be used instead (afaik this would be only possible with some flags indicating that its a fake populating event triggered by my mouseover and after PopulatingEventArgs.Cancel i should call immediately PopulateComplete. but i dont get it, if this may work (haven't tried yet), why not when calling simply the PopulateComplete)?
Well, I tried the IsDropDownOpen with a testing bit, and almost worked:
private void FoodBox_MouseEnter(object sender, MouseEventArgs e)
{
//FoodBox.PopulateComplete(); not working after acb loses focus...
testbit = true;
FoodBox.IsDropDownOpen = true;
}
Here's the overloaded Populating method (no need for setting ItemsSource explicit because its bound to an ObservableCollection):
public void FoodBox_Populating(object sender, PopulatingEventArgs e)
{
e.Cancel = true;
if (!testbit)
{
VM.LoadFoodSuggestions(FoodBox.SearchText);
}
else
{
testbit = false;
FoodBox.PopulateComplete();
}
}
This works good so far, execpt that the search does not start because when (for the first time) you mouseover and select the acb, it sets the testbit to true.
So I added another event handler that takes care of setting the testbit to false every time the user inputs text on the keyboard, ensuring that the suggestions are regenerated/reloaded after SearchText is modified by the user, but not when you select an item from the dropdown:
private void FoodBox_TextChanged(object sender, RoutedEventArgs e)
{
testbit = false;
}
I still don't know why calling PopulateComplete() isn't enough without setting the IsDropDownOpen to Ture, and setting that to true, also delays the dropdown opening approximately with the time specified in the MinimumPopulateDelay, but at least it gives me the functionality I wanted. (Maybe digging into the source of acb would answer this mistery)
Maybe this functionality implemented in the basic acb would be helpful in a future release of the control.
I'm attempting to use the ScintillaNET control in an application I am working on. I drag and drop the control into my form and run form. The control appears on the form. This is good. In addition, if I set any of the properties in the control's properties editor (ConfigurationManager.Language, for example), I am able to type in that language and see syntax highlighting occur.
Where I run into problems is when I attempt to change properties programmatically. For example, I attempt to load text from a file into the form (I'm doing this in the form's Load). The text doesn't display. I also can't seem to show the line numbers or do any other number of tasks (including programmatically change the Language).
Any idea what I may be doing wrong? Even something as simple as the code below doesn't seem to work:
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
}
Simply add scintilla1.ConfigurationManager.Configure();
private void scintilla1_Load(object sender, EventArgs e)
{
scintilla1.ConfigurationManager.Language = "xml";
scintilla1.ConfigurationManager.Configure();
}
After spending some time playing around with the different events, it appears that I cannot affect the Scintilla control until after it is already visible. Hence, the "Load" event does not let me make any programmatic changes to the control until I've set it visible.
It's a little strange, and seems sort of pointless to me to have the Load event at all, but I just wanted to let everybody know what is happening in case someone else ran into the same problem.