I have a list box that looks like this:
<ListBox
ItemsSource="{Binding LoadingModules}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Opacity="{Binding ModuleLoaded, Mode=TwoWay,
Converter={StaticResource BoolToDoubleConverter}}"
Text="{Binding ModuleName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The idea is that any item with ModuleLoaded = true should have 20 percent opacity.
This works great at design time. But at run time this does not happen.
This is my logic where I actually change the value:
private void OnModuleLoaded(ModuleInfo moduleInfo)
{
LoadingModules.Where(x => x.ModuleName == moduleInfo.ModuleName)
.FirstOrDefault().ModuleLoaded = true;
}
Any my LoadingModule class has ModuleLoaded defined like this:
private bool moduleLoaded;
public bool ModuleLoaded
{
get { return moduleLoaded ; }
set
{
if (!object.Equals(moduleLoaded , value))
{
moduleLoaded = value;
OnPropertyChanged("ModuleLoaded ");
}
}
}
The only way I have been able to get it to work is by doing this:
var foundModule = LoadingModules.Where(x => x.ModuleName==moduleInfo.ModuleName)
.FirstOrDefault();
LoadingModules.Remove(foundModule);
foundModule.ModuleLoaded = false;
LoadingModules.Add(foundModule);
This does work, so I know that my bindings and converter are wired up.
But removing and re-adding is not really a solution. And it causes problems because my events don’t arrive in perfect order.
Is there any way I can get my ListBox.ItemTemplate DataTemplate to update on the fly based on a binding?
You are passing
"ModuleLoaded "instead of"ModuleLoaded"to yourOnPropertyChangedcall. This means that even though your property value changes, the UI does not get informed about it and thus retains its original state.Other than that, your
Wherecall is redundant. Instead of callingyou can simply call