C# Binding a Property but checking another [duplicate] - c#

Is it possible to bind to a value in Properties.Settings.Default in a way that will keep my UI current with the value stored there?
I have a class:
public class FavoritePlayer
{
public string Name
{
get
{
return wpfSample021.Properties.Settings.Default.FavoritePlayer.ToString();
}
set
{
wpfSample021.Properties.Settings.Default.FavoritePlayer = value;
}
}
}
In my XAML, I have a resource:
<local:FavoritePlayer x:Key="fvPlayer"/>
and a binding:
<Label DataContext="{DynamicResource fvPlayer}" Content="{Binding Path=Name}"/>
What I would like is for the data-binding to update any time the property is changed. Is there a way to do this?

Conveniently for you, ApplicationSettingsBase implements INotifyPropertyChanged so you just need to subscribe to PropertyChanged on Properties.Settings.Default and raise your own PropertyChanged in response.

You need your properties to implement INotifyPropertyChanged in order for bindings to recognise when the property value changes.

Related

Data binding in wpf with expression in getter

I have a property in my model which has getter
public boolean Status {
get {
return 1==2;
}
}
and a label
<Label Content="{Binding Path=Status, Mode=OneWay}" />
I would like to monitor the Status. Now when the Status value is changed UI does not get updated with the value.
Assuming, that view model already implements INotifyPropertyChanged, you must raise PropertyChanged event, if you want UI to re-read property value. It doesn't matter if this is get-only property or get/set one.
Since it's get-only, you need to call OnPropertyChanged after you change something, that affects getter expression result:
private int a;
private int b;
public bool Status => a == b;
private void DoSomething()
{
a = ...;
b = ...;
// ...
OnPropertyChanged(nameof(Status));
}
You will have to do two things:
Implement the INotifyPropertyChanged interface in class which contains the Status property.
Call the PropertyChanged handler and call that method in the setter of the Status property which you will implement as a part of INotifyPropertyChanged - if you don't what it is, please google it and you will get the piece of code - it basically notifies the UI that a property has changed.
If the Status property is only get then you will have to call the PropertyChanged method after you know that the status has been updated.

WPF: Binding TextBox Text to a sub-element of a Property with WCF?

I have a TextBox which I'm trying to bind to a element of a table property 'regimeAlias' is a column with the tbRegimes table which I have mapped with Entity Framework:
<TextBox Text="{Binding NewRegime.regimeAlias, Mode=TwoWay}"/>
Exposed property in my ViewModel:
private tbRegime _NewRegime;
public tbRegime NewRegime
{
get { return _NewRegime; }
set
{
_NewRegime = value;
OnPropertyChanged("NewRegime");
}
}
Lastly, here's the WCF Service Reference auto-generated code class:
public partial class tbRegime : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
//blah blah blah
[System.Runtime.Serialization.DataMemberAttribute()]
public string regimeAlias {
get {
return this.regimeAliasField;
}
set {
if ((object.ReferenceEquals(this.regimeAliasField, value) != true)) {
this.regimeAliasField = value;
this.RaisePropertyChanged("regimeAlias");
}
}
}
The setter never gets hit. Is this because each element within the NewRegime object needs to raise PropertyChanged and if so is there an easy workaround without adding a further DTO layer to my code?
Edit3: with the post from your regimeAlias code. i have to say your binding should work. but of course if you wanna debug you have to set the breakpoint in your regimeAlias setter
<TextBox Text="{Binding NewRegime.regimeAlias, Mode=TwoWay}"/>
this code means, you bind to a Public Property regimeAlias in your class tbRegime.
your setter for NewRegime will never hit because you dont bind to it.
so check your tbRegime class property setter for regimeAlias.
EDIT: the DataContext of the TextBox is of course an object with the Public Property NewRegime, but like i said if you use dot notation in your binding the last property is the one you bind to :)
EDIT: you dont have much ways to workaround:) if you let the binding like you did, you need a model with a public property regimeAlias and it should implement INotifyPropertyChanged.
if you wanna wrap the regimeAlias Property then you have the problem the you have to raise OnPropertyChanged("MyRegimeAlias") at the right point.
public string MyRegimeAlias
{
get { return _NewRegime.regimeAlias; }
set
{
_NewRegime.regimeAlias = value;
OnPropertyChanged("MyRegimeAlias");
}
}
xaml
<TextBox Text="{Binding MyRegimeAlias, Mode=TwoWay}"/>

Silverlight CustomControl dependency property can't be bound to parent viewmodel

I'm having a custom Control that has a dependency property
public static readonly DependencyProperty SelectedUserCodeProperty = DependencyProperty.Register(
"SelectedUserCode",
typeof(decimal),
typeof(SystemUsersControl),
new PropertyMetadata(SelectedUserCodeChanged));
public decimal SelectedUserCode
{
get
{
return (decimal)this.GetValue(SelectedUserCodeProperty);
}
set
{
this.SetValue(SelectedUserCodeProperty, value);
RaisePropertyChanged("SelectedUserCode");
}
}
This control is inside another usercontrol that I'm attempting to get the dependency property above in its viewmodel
this xaml is inside the parent control
<SystemUsers:SystemUsersControl Name="ctrlSystemUsersControl" SelectedUserCode="{Binding SelectedSystemUserCode, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,2,0,0"/>
but nothing is bound to the parent control viewmodel
I don't know what's the problem, it's my first time dealing with dependency properties, I'm considering making the two controls in one :( unless I got any help :)
Don't worry,
SelectedSystemUserCode must be a property . If its a property you will see initial value ,but what will fully support binding for your class is ,implementation of INotifyPropertyChanged. This basic interface will be a messenger for us.
1)When you implement INotifyPropertyChanged,the below event will be added to your class.
public event PropertyChangedEventHandler PropertyChanged;
2)Then create a firing method
public void FirePropertyChanged(string prop)
{
if(PropertyChanged!=null)
{
PropertyChanged(prop);
}
}
3) Register this event for not getting null reference.
in constructor this.PropertyChanged(s,a)=>{ //may do nothing };
4) //You may use Lazy < T > instead of this.
public decimal SelectedSystemUserCode
{
get{
if(_selectedSystemUserCode==null)
{
_selectedSystemUserCode=default(decimal);
}
return _selectedSystemUserCode;
}
set
{
_selectedSystemUserCode=value;
FirePropertyChanged("SelectedSystemUserCode");
//This will be messanger for our binding
}
}
In addition,
As I remember is the default value so you may give a decimal value for that,SelectedUserCodeChanged is callback method its ok also.
//new PropertyMetadata(SelectedUserCodeChanged)
new PropertyMetadata(0) or null
Hope helps.

ItemsControl: Binding an item's property to the index?

I have a collection of custom objects and i want to bind the index property of the ItemsControl to one of the int property in my custom object. how do i define such binding in the template? do i need a converter? any suggestions? thanks
First problem: ItemsControl doesn't have an Index or SelectedIndex property. For that, you need something that derives from Selector (like ComboBox, ListBox, etc.).
In that case, you can accomplish what you want easily using the SelectedValue and SelectedValuePath properties.
public class MyCustomObject {
public int CustomObjectIndex {get;set;}
}
public class ViewModel : INotifyPropertyChanged {
public IEnumerable<MyCustomObject> Items {get { return something;} }
// Setting this must raise PropertyChanged.
public int SelectedIndex {get; set; }
}
<ComboBox ItemsSource={Binding Items}
SelectedValue={Binding SelectedIndex, Mode=TwoWay}
SelectedValuePath="CustomObjectIndex" />
what you want to do doesnt make sense...
imagine you have a custom object with properties (name, wishedIndex) (wishedIndex as integer or whatever other magic to evaluate the wished index)
and now you have several of these objects --> several of wished indices.
Somewhere in your architecture you made a bad design choice. if you post more code we can find out

WPF - Checkbox bound to whether or not ObservableCollection contains a value?

I currently have a checkbox that is bound to a property that checks an ObservableCollection for a specific value. If the value exists, then the property returns true.
My property and the property that exposes the ObservableCollection are both readonly.
This approach works fine when I first load my model, but when it add additional items into the ObservableCollection the checkbox that is bound to the property does not update.
Here's the code for my property:
public bool IsMeasure11
{
get //readonly
{
return this.Charges.Any(t => t.IsMeasure11);
}
}
And here's the code for my ObservableCollection:
public ObservableCollection<DACharge> Charges
{
get //readonly
{
if (_charges == null)
{
_charges = new GenericEntityCollection<DACharge>(_DACase.Id).ToList().ToObservableCollection();
}
return _charges;
}
}
And the XAML for the Checkbox:
<CheckBox Content="M11" Name="chkM11" IsChecked="{Binding IsMeasure11, Mode=OneWay}">
Thanks in advance,
Sonny
Your property does not implment INotifyPropertyChanged so there is no way for the binding engine to know to update the bound property.
An ObservableCollection<T> does not update the property which it is being returned within; in this instance the public ObservableCollection<DACharge> Charges property.
You will need to register for the ObservableCollection.CollectionChanged event and when an item is added/removed, etc...fire a PropertyChanged event as displayed in the previous link for the Charges property.
You need to invalidate the binding of IsMeasure11 (i.e. invoke the PropertyChanged event of INotifyPropertyChanged) whenever the Charges collection changes. One way of doing this would be to subscribe to the CollectionChanged event of the Charges collection.
// somewhere, maybe the constructor of your view model
this.Charges.CollectionChanged += ChargesChanged;
private void ChargesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.NotifyOfPropertyChanged(() => IsMeasure11);
}
You need to make your "add item" action to fire up the INotifyChanged Interface's PropertyChanged event as described here: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
If not then WPF will never know when the value changes as the binding isnt sourced at the ObservableCollection but to another property that accesses it instead.
Hope it helps.

Categories