XAML data binding - user interface is not automatically updated

I created a program that stores any number of objects of my type Project. Each project then contains any number of Files, which is another object that I created for this program.

The problem that I am experiencing in XAML is in two areas, but in my opinion has a similar origin.

I have a window that contains a ListView populated with files in the selected project. From here I can check the box next to each to turn them on or off, and if I select a file, information about it will appear in the status bar of this window.

If I turn off the file, its text color should look light gray in the ListView, but it does not do it automatically; I need to close the window and open it again. The file implements INotifyPropertyChanged and fires this event if the on / off state changes.

I use this XAML code, where the converter is in my code behind the class:

<ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Foreground" Value="{Binding Path=IsVisible, Converter={StaticResource VisibleStateToFontColourConverter}}"/> </Style> </ListBox.ItemContainerStyle> 

Also for the selected file, if the information in the file changes during its selection (which other classes may occur), I want the status bar to be automatically updated to reflect this change, but it is not; I have to click something else and then reselect the file of interest. I implement and use INotifyPropertyChanged for this too, so I don’t know why it is not updated automatically. My XAML code for the status element is as follows:

 <StatusBarItem Name="statusItem_FileInfo" Content="{Binding ElementName=loadedFiles_ListView, Path=SelectedItem, Converter={StaticResource GIS_FileToInfoConverter}}"/> 

Does anyone know what I am missing, what will bring it all together?

+4
source share
2 answers

Try adding UpdateSourceTrigger=PropertyChanged to your binding:

 Value = "{Binding ... , UpdateSourceTrigger=PropertyChanged}" 

Call OnPropertyChanged immediately after changing the property with the name of the changed property:

 public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler == null) return; handler (this, new PropertyChangedEventArgs(propertyName)); } 

If you change the "IsVisible" call to OnPropertyChanged("IsVisible")

+8
source

why don't you use datatrigger for your list item? if I get you right, do you just want to switch between the two states?

therefore the default style should be foreground = black and datatrigger. Property = IsVisible Value = false β†’ then the frontround lightgrey

0
source

Source: https://habr.com/ru/post/1413746/


All Articles