Reputation: 605
Let's say I'm binding to a control's visibility:
Visibility="{Binding Path=Name, Converter={StaticResource NameToVisibilityConverter}}"
With this, it works well when the application first starts up, but when a certain parameter within the NameToVisibilityConverter
changes that causes the visibility to be different, but the Name
that the visibility was bound to stays the same, the converter isn't retriggered. I could try this:
Visibility="{Binding Path=Name, Converter={StaticResource NameToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}"
Adding a UpdateSourceTrigger=PropertyChanged
doesn't help this situation since the Binding Path=.
is the same and won't trigger the valueconverter to convert again.
How can I change the property of the UpdateSourceTrigger=PropertyChanged
so it triggers on a different property than Name
(what I want to achieve is like an UpdateSourceProperty
property different from the current binding):
Visibility="{Binding Path=Name, Converter={StaticResource NameToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged, UpdateSourceProperty={Binding Path=Count}"
Thank you! Please let me know if anything needs to be clarified. Bottom line is that I need a valueConverter to convert more often than how often the binding property changed.
Upvotes: 1
Views: 1566
Reputation: 169420
How can I change the property of the U
pdateSourceTrigger=PropertyChanged
so it triggers on a different property than Name (what I want to achieve is like anUpdateSourceProperty
property different from the current binding):
The converter is only invoked when the data-bound property changes. Changing the value of the UpdateSourceTrigger
doesn't change anything.
What you could do is to bind to more than one property using a MultiBinding
and a multi value converter, e.g.:
<SomeControl.Visibility>
<MultiBinding Converter="{StaticResource NameToVisibilityConverter}">
<Binding Path="Name" />
<Binding Path="SomeOtherPropertyThatShouldTriggerTheConverter" />
</MultiBinding>
</SomeControl.Visibility>
Your converter class should then implement the IMultiValueConverter
interface instead of the IValueConverter
interface.
Upvotes: 3
Reputation: 1031
First, UpdateSourceTrigger has nothing to do with the effect you need. It is for updating the source (Name in your case) when the property is changed in your window. If you want the property to change on changing the source you should, firstly, implement the INotifyPropertyChanged interface in your model class.
class Model
{
private string name;
public string Name
{
get=>name;
set
{
name = value;
OnPropertyChanged(nameof(Name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
Then if you bind a Name property to some control property in you window, every time this property is set, it will fire PropertyChanded
event for the Name
property and the binding will be updated.
If the property depends on several properties in the model and should be updated when any of the model properies is changed, the most logical way is to use MultiBinding
with IMultiValueConverter
. But if for some reason you don't want or can not do it, you can fire
OnPropertyChanged(nameof(Name))
every time you change not only Name, but also other properties your control depends on. Then the binding will update as if you've changed the Name.
Upvotes: 0