WPF CheckBox.IsChecked binding - c#

I have a datagrid with a variable number of columns that I am generating programatically. It contains DataGridTemplateColumns, each with a DockPanel containing a CheckBox and a TextBlock.
Binding code:
Binding bindingPicked = new Binding(string.Format("Prices[{0}].Picked", i));
bindingPicked.Mode = BindingMode.TwoWay;
CheckBox code:
FrameworkElementFactory factoryCheckBox = new FrameworkElementFactory(typeof(CheckBox));
factoryCheckBox.SetValue(CheckBox.IsCheckedProperty, bindingPicked);
Picked property:
private bool _picked;
public bool Picked
{
get { return _picked; }
set { _picked = value; }
}
When the datagrid is initialized, the Picked getters are called as expected. However, when I check/uncheck a checkbox, the setter isn't called. What is causing this? I do not want to use a DependencyProperty, and I don't think it should be needed as I just need the property setter to be called when the user clicks the CheckBox.
EDIT: Apparently I am a moron, I simply forgot
bindingPicked.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
Feel free to close this.

bindingPicked.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
That should do it :)

I Think you should Implement INotifyPropertyChanged's and call the event in set

As above, you need to implement INotifyPropertyChanged
The correct pattern to follow is:
private bool _picked;
public bool Picked
{
get { return _picked; }
set
{
if (_picked != value)
{
_picked = value;
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs("Picked"));
}
}
}
}
The UpdateSourceTrigger property tells databinding when to update the source.
For example, with a TextBox, the default is LostFocus. For most other controls it is PropertyChanged.

Related

set IsEnabled = false a button in silverlight

I have the following code in the ViewModel class, in the constructor where I define that the buttons are always Enabled = false when starting the form ...
public partial class EditarConceptoWindow : ChildWindow
{
public EditarConceptoWindow(string documentoId)
{
InitializeComponent();
viewModel.Saved += new EventHandler<Microsoft.Practices.Prism.Events.DataEventArgs<bool>>(ViewModel_Saved);
viewModel.Calculation += new EventHandler<Microsoft.Practices.Prism.Events.DataEventArgs<bool>>(ViewModel_Calculation);
this.DataContext = viewModel;
BtnCalcular.IsEnabled = false;
BtnObtenerTCRM.IsEnabled = false;
....... rest of code
In a checked event of a check box when placing the Selected check box, it must be enabled to be set to true, depending on whether a particular element of a combobox has been selected as well;
private void cbAgregarManual_Checked(object sender, RoutedEventArgs e)
{
if (this.ComboConcepto.SelectedValue.ToString() == "DPI")
{
BtnCalcular.IsEnabled = true;
BtnObtenerTCRM.IsEnabled= true;
}
}
This must be done if and only if the checkbox is clicked and the DPI value is selected in the combobox.
But the behavior of the buttons is that when starting the form they are always IsEnabled = true and if the checkbox control is clicked if it works but I can't find a reason because only until I click the checkbox it works, there are some controls (such as TextBoxes, and also the buttons) with this directive in the XAML.
IsEnabled="{Binding ElementName=cbAgregarManual, Path=IsChecked }"
I suggest that you centralize the logic of the enabling into one property to avoid this mismatch of logic setting confusion.
In this new property it will use INotifyPropertyChanged for the notification of that change, but called in from other properties. So to sum up, when any of the associated values change, they do the notify call on the logic property; that process ensures that the control is properly en/dis-abled.
Example
Such as this pseudo code where I check three different other properties:
public bool IsEnabledCombo { get { return ClickStatus
&& OtherStatus
&& !string.IsNullOrEmpty( UserText); }
public bool ClickStatus { get { return _clickStatus; }
set { _clickStatus = value;
NotifyPropertyChanged("ClickStatus");
NotifyPropertyChanged("IsEnabledCombo");
}}
public bool OtherStatus { get { return _otherStatus; }
set { _clickStatus = value;
NotifyPropertyChanged("OtherStatus");
NotifyPropertyChanged("IsEnabledCombo");
}}
public string UserText { ...
set { _userText = value;
NotifyPropertyChanged("UserText");
NotifyPropertyChanged("IsEnabledCombo");
Bind your control as such
IsEnabled="{Binding IsEnabledCombo }"
So wherever one of the related values can change they also call NotifyPropertyChanged("IsEnabledCombo"); and the control status is updated automatically.
I provide a another notify chained example doing similar on my blog:
Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding

WPF : MvvmLight, Usercontrol, Binding

I have a simple usercontrol (DoubleRadioControl2), composed of 2 radio buttons. I have a Dep Prop on this UC : (bool?)IsOuiChecked :
true - 'yes' radio checked
false - 'no' radio checked
null - both
radios unchecked
Pretty simple.
private static readonly DependencyProperty IsOuiCheckedProperty = DependencyProperty.Register("IsOuiChecked", typeof(bool?), typeof(DoubleRadioControl2), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public bool? IsOuiChecked
{
get
{
return (bool)GetValue(IsOuiCheckedProperty);
}
set
{
SetValue(IsOuiCheckedProperty, value);
if (value == null)
{
RadioYes.IsChecked = false;
RadioNo.IsChecked = false;
}
else
{
RadioYes.IsChecked = (bool) value;
RadioNo.IsChecked = !(bool) value;
}
}
}
And the logic between ths 2 radios :
private void OptDecompteConfnOui_Click(object sender, RoutedEventArgs e)
{
IsOuiChecked = true;
}
private void OptDecompteConfnNon_Click(object sender, RoutedEventArgs e)
{
IsOuiChecked = false;
}
When i bind this UC to my viewmodel, the prop binded if updated when i click on my radios. But when i set the value in the code of my view model, it won't update my radios.
In my viewmodel :
private bool? _isDRB2OuiChecked;
public bool? IsDRB2OuiChecked
{
get
{
return _isDRB2OuiChecked;
}
set
{
if (_isDRB2OuiChecked == value)
{
return;
}
_isDRB2OuiChecked = value;
RaisePropertyChanged(() => IsDRB2OuiChecked);
}
}
TwoWay binding not working.
The getters and setters for your dependency property only exist for your (the programmers) convenience. WPF itself will not call them, but set the property directly.
You need to attach a handler to the changed event of the property. Use one of the FrameworkPropertyMetadata constructors, that take a PropertyChangedCallback . Your getter and setter logic needs to be handled there instead of inside the property.
If you are using MVVM you should call OnPropertyChanged
First of all this is a very BAD practice to put code inside the Get/Set parts of a dependency property, look at these exanples to get the solution. If you have the logic on DP changes, you have to put that login inside the callback.
Dependency properties explanation here.
MSDN Overview here.
if there is binding defined at the View/Viewmodel level you have to use a INotifyPropertyChange event at the Viewmodel side to inform View that the binded property was changed.
regards,

OnPropertyChanged not working as expected with ObjectListView

Here is my model class, the column that I am interested in this question:
public class Cell : INotifyPropertyChanged
{
public string TestImageAspect
{
get { return testImageAspect; }
set
{
testImageAspect = value;
Console.WriteLine("OnPropertyChanged => testImageAspect");
this.OnPropertyChanged("OperationResult");
}
}
private string testImageAspect;
}
ImageList is prepared with required images. In the ObjectListView I set appropriate column's ImageAspectName to the property name:
Then on button click I run the following code to change the
Cell c = ...;
c.TestImageAspect = "success"; // the name exist in ImageList
After above code I see that OnPropertyChanged has been called, however UI is not updating, unless I hover to the row where it has to change, then I see new icon. I am not looking for dirty workaround, since I know few, but rather want to understand whether ObjectListView has to update UI itself. If yes, what am I doing wrong?
The ObjectListView property UseNotifyPropertyChanged has to be set true.
From the official documentation
If you set UseNotifyPropertyChanged, then ObjectListView will listen for changes on your model classes, and automatically update the rows when properties on the model classes changed. Obviously, your model objects have to implement INotifyPropertyChanged.
Could you post the XAML for the binding - that might help debug this. Also, it's bit confusing that your property is called TestImageAspect but you're passing "OperationResult" to OnPropertyChanged. I'm not sure if OnPropertyChanged would work either. The more usual way would be to do:-
public class Cell : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string TestImageAspect
{
get { return testImageAspect; }
set
{
testImageAspect = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("TestImageAspect"));
}
}
}
private string testImageAspect;
}

Binding ListBox.SelectedItem to Property

This might be a duplicate question, but I'm unable to find a good answer. All the answers like Binding WinForms ListBox to object properties don't work on my WinForm. I'll explain.
I have a list of Firms that I show in a ListBox. I would like when the SelectedItem changes, that it updates a property on my model. So that I can read the Firms properties.
// the classes
public class Firm
{
public string Name { get; set; }
public int Id { get; set; }
// more properties ...
}
public class MyModel : INotifyPropertyChanged
{
private Firm _firm = new Firm();
public Firm Firm
{
get { return _firm; }
set
{
if (Equals(value, _firm)) return;
_firm = value;
OnPropertyChanged();
}
}
// more properties and OnPropertyChanged() ...
}
// the form
private MyModel Model;
public void MyForm(List<Firm> firms)
{
lstFirm.DataBindings.Add("SelectedItem", Model, "Firm",
true, DataSourceUpdateMode.OnPropertyChanged);
lstFirm.DisplayMember = "Name";
lstFirm.ValueMember = "Id";
lstFirm.DataSource = firms;
}
public void lstFirm_SelectedIndexChanged(object sender, EventArgs e)
{
// Do something with Model.Firm
}
The problem is that Model.Firm null is. Does anybody have an idea what I need to do to make a databinding between the ListBox and the Model? I bind other stuff on my WinForm (such as TextBoxes to String properties) and those work nicely.
From what I can see, your code never sets Model.Firm... Where's the constructor for MyModel? If you don't provide one, Model.Firm will stay null unless you explicitly set it. Here's an example constructor:
public MyModel(Firm firm)
{
_firm = firm;
}
Also, Equals() doesn't do what you think it does. Instead of if (Equals(value, _firm)) return;, use this: if (value == _firm) return;
Ok, so after a weekend of testing, I figured it out.
I was debuging in the SelectedIndexChanged event and didn't see the change in my Model.Firm just yet. But as the SelectedItemChanged event is only internal, I couldn't use that and that's where the databinding on SelectedItem applies the values to databound items.
Now the reason why the change isn't visible yet, is because the SelectedItemChanged is only fired after the SelectedIndexChanged is executed. So internally in the ListBox control, it probably looks like
this.SelectedIndex = value;
this.SelectedItem = FindItem(value);
this.SelectedIndexChanged(/*values*/);
this.SelectedItemChanged(/*values*/); // Apply databinding changes
So it's quite normal that you don't see the changes, before the change has occured. And I didn't know this, so I was kinda stumped why the SelectedItem (who was displaying the changed value) wasn't copied over to the databound model property.
So I didn't have to change anything major to get it all working. :)

Binding Dependency Properties

So here I come creating a user control. It consists of a treeview dropping down from a combobox.
Actually, there is a button with a control (DropTree) dropping down from its contextmenu. So I have a control DropTree.
public partial class DropTree : UserControl
{
public TreeView TreeView
{ get{return treeView;} }
public DropTree()
{ InitializeComponent(); }
}
to simplify it, I made the TreeView control public, then I have my main control which is called ComboTreeView.
Now I need to represent some treeview properties in it, so I define several dependency properties:
public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ComboTreeView), new FrameworkPropertyMetadata { Inherits = true, IsNotDataBindable = false, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
and in constructor it is:
public ComboTreeView()
{
InitializeComponent();
TreeViewControl.SetBinding(TreeView.SelectedItemProperty, new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay });
}
and it all seems ok, until i run it. It crashes saying that SelectedItem cannot be binded to data. I don't understand?
The same goes for ItemsSource and SelectedValue... but only SelectedValuePath property defined this way goes fine.
Can anybody help? Or is there any other way to bind it correctly?
PS: by the way, I need to use DataBinding for ComboTreeView in my code later.
Try to set the Binding on SelectedValue instead of SelectedItem.
TreeView.SelectedItem is a readonly property. You can't set it, whether explicitly or through binding. In order to select a node in a TreeView, you must set the TreeViewItem.IsSelected property to true.

Categories