UserControls: What am I missing? - winforms

I have created what I call a "LabeledTextBoxWithLookupControl" which inherits from UserControl. I've put several of these controls on a form.
Now I'm beginning to think that this was a bad idea.
When I call TopLevelControl.SelectNextControl(this, true, true, true, true) from a KeyPress event assigned to the TextBox in the UserControl, it's actually selecting the next UserControl, where I actually want it to select the text box inside the next user control. The UserControl's CanSelect property returns true, but I don't see any easy way to change a UserControl's Control Style.
Also, when I use the Tab Order mode, I'd prefer it not consider the user control itself, but the the just the TextBox in inside the UserControl as candidate for tab order. Should I override the TabIndex and TabStop properties of the UserControl and make them point to the TabIndex and TabStop of the TextBox?
Also, should I expose just the properties of the controls themselves, or should I expose each control as a property to set these properties I want to be able to set, such as the Text property of the label control, the click event of the LookupControl and the Text property of the TextBox.

If you don't want the UserControl to be a tab stop but rather it's contents, you have to make sure that it doesn't have the ControlStyles.Selectable style.
In the constructor of your UserControl add:
SetStyle(ControlStyles.Selectable, false);

Related

Fire an event when a property dependency in another user control changes

I have a custom treeview inside a user control. It exposes a dependency property that represents the currently selected treeviewitem. In the same window i have another user control that should change content of it's controls when the selected item changes. I need to do this changes from code behind. How do i fire up an event in the second user control when the selected item changes in the first one?
As the Dependency property whose change you want to be notified about is one created by yourself, why don't you register a PropertyChangedCallback for that property in your user control? This way, you can add a SelectedItemChanged event to the control and raise it from the PropertyChangedCallback code.
Any object that needs to know when the selectedItem has changed just has to listen to this new event.

CheckBox UserControl Always Checked

I have a usercontrol called RateView.xaml.cs. This user control contains a checkbox called CheckBox1.
I put this user control on my MainWindow 5 times. I need three of the usercontrol's on the MainWindow to have the checkbox on from the start of the app. How can this be accomplished?
Thanks
Create a dependency property IsChecked on the UserControl, bind it to the internal CheckBox, set that property accordingly on the instances of the UserControl.

Commands that act on two controls

Looking through the standard WPF commands, such as copy/paste, they seem to all work using one button and act on a textbox.
My question: how do I use commands when I have one button, but I need data to be set in two separate controls(a textbox and a combobox). If the user has written text in textbox, but not selected a combobox value, then CanExecute should fail. This applies if combobox has been set, but not the textbox.
In my case specifically, all these controls are wrapped in a tabitem. As well, I have another tab with only a textbox and a button. I want it to have the same functionality as the first tab, except, instead of checking for the combobox value, it should detect that there is no combobox and pass in a default 'null object' value instead.
The Execute method should call a method in my viewmodel and pass in values from the combobox and textbox. As well, each tab should pass in another unique static value; i think i can handle this using commandparameter though.
How do I make this work? Do I make the parent tab that commandtarget and directly reference its children controls in the can/execute methods?
You need to implement CanExecute method that checks both TextBox databinding value and ComboBox.SelectedItem databinding value.
Take example from your question.
Your TextBox.Text should be databinding to your ViewModel
And as well as your ComboBox.
So your ViewModel should have two Properties:
public string TextBoxCurrentText {get { ...}set {...}}
public string ComboBoxCurrentSelected {get { ...}set {...}}
Then in both Setter, you would do your YourCommmand.RaiseCanExecuteChanged();
So it will execute your CanExecute code piece to determines can your Button be click.
Which can be:
bool YourCommandCanExecute()
{
//Just example
if (!string.IsNullOrEmpty(TextBoxCurrentText) && !string.IsNullOrEmpty(ComboBoxCurrentSelected))
return true;
return false;
}

How can I set the keyboard focus to the content of a ContentPresenter when it changes?

I have a ContentControl in my view which is databound to the CurrentItem property of my viewmodel. The objects that are exposed via CurrentItem each has its own DataTemplate.
When the CurrentItem property changes, the appropriate DataTemplate for that item is displayed, as it should be. However, I cannot find out a way to set the keyboard focus to the content of the DataTemplate.
Even if I manually set the keyboard focus to the DataTemplate, if the CurrentItem property changes (and a new Template is instantiated) the focus is lost (FocusManager.GetFocussedElement returns null).
How can I set the keyboard focus to the content of the ContentPresenter when it changes?
I believe you can use the LayoutUpdated event on either your ContentControl or the ContentPresenter. This should fire anytime the Content/ContentTemplate/etc changes.
Alternatively, you could derive a class from ContentControl then override the OnContentChanged, OnContentTemplateChanged, etc methods. Then you'd need to search down the visual tree and set the focus. You may need to use the Dispatcher to delay the focus setting code.

WPF TabControl add extra tabs to a bound control

I have a Tab Control with a ItemsSource Binding.
...
I want to add a predefined tab to the front called All that has an aggregate of all the other tabs, and I would also like to add a button at the end called Add so I can add a new tab.
Is there an easy way of doing this ?
Thanks,
Raul
The easiest way is to go with MVVM (the example in the url acutally contains TabControl bound to a ViewModel). Your ViewModel that you bind your TabPages against could expose an observablecollection of items where the first item is always a ViewModel instance that holds you aggregate data. All follwing items are the ViewModel instances for the rest of the tabpages. Your ViewModel would also expose a ICommand AddTabPage wich adds a new item to the obeservablecollection. The TabPage will pick up this change automatically. You'd have a button whose Command property is bound to this command.

Resources