CheckBox UserControl Always Checked - wpf

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.

Related

wpf checkbox binding with user controls

I am working on wpf mvvm pattern. I have different user controls. Based on the checkbox selection, I want them to be loaded in the main screen (that is also a user control). I have one HomeViewModel class which I have been using to bind the user controls of my project. Can you help me with a suitable way?
You should have different ViewModels for each kinds of UserControl.
Create different DataTemplates for each ViewModel Types
Put a ContentControl with binding a property of HomeViewModel -
Value of property will be an instance of ViewModel ( UserControl's) and is set by toggling CheckBoxes.

Multibinding not firing when the bound properties change

I have a wpf wpplication with a number of user controls in it. One of these controls has a property called ButtonsEnabled. This is a bool DependencyProperty in the user control. The property is bound to the IsEnabled property of a couple of buttons on that control.
This user control is used in the MainWindow. The MainWindow has a couple of view model objects in it called EocMonitor and ComMonitor. These both descend from an abstract base class that implements INotifyPropertyChanged. The ButtonsEnabled property on the UserControl is bound to the Status property using a multibinding and a class that implements IMultiConverter that I wrote.
The problem is that even though the PropertyChanged event is being raised when the Status property changes, the IMultiConverter is not being called after it is initially called, so the value of the ButtonsEnabled property is not changing. As a result, the buttons are not enabling.
What do I need to do to make this work?
I did an end-run around this problem as I am running out of time before we reach our code-freeze for this release. What I did was I added a ButtonsEnabled DepdendencyProperty to the MainWindow class and bound it to the user control's ButtonsEnabled property. I then added a PropertyChanged event handler in the MainWindow and and registered it with the DbMonitor and ComMonitor objects when they were created. I then wrote code in the PropertyChanged event handler to set the MainWindow's ButtonsEnabled properly.
Everything works and I'll worry about making the other approach work at some later time. Maybe.

Retrieves value from Wpf UserControlLibrary to WpfForm

In my WPF application, I have a usercontrol library. In this library, I have a listview control along with textbox and button controls.
I have placed this usercontrol in a WPF window form. How can I retrieve the listviewitems of my usercontrol in this WPF window form?
You ask a very general question. There are dozens of ways to do that, depends on your needs. For example: when you place you UserControl within the Window - you should name it (e.g. . Then, within the UserControl, you can have a public function, or a public getter, which will return the items (it is the 'Items' property of the ListView).
Another way is binding. If you have a ViewModel, or any other class as the DataContext, you can bind the ItemsSource of the ListView to a collection on your ViewModel. Unless you change the DataContext of the UserControl - it will have the same DataContext as the main Window.

UserControls: What am I missing?

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);

Controls in a WPF UserControl don`t raise lost focus

I have a MainWindow with 3 main buttons at the top and below a MainUserControl.
In the MainUserControl I have at top 3 UserControls with ButtonBars
and at the bottom a DataGrid.
When I enter data in a DataGridCell and I click into another cell a property change is fired in my ViewModel bound to the DataGrid.
When I enter data... and I click on one of the 3 main buttons again a property change is fired because of Lost Focus event.
When I ... and I click on one of the buttons in the ButtonBar in the UserControl no property change is fired because there seem to be no Lost Focus event.
How can I fix that?
FocusManager.IsFocusScope="False" on the UserControl or other elements like Menu solved the problem and my property changes are raised now in the model :)
If you change your binding to to set UpdateSourceTrigger=PropertyChanged then you will not need to rely on the LostFocus to do a property update.

Resources