Hi,
I've a Items that is an ObservableCollection<Item>.
Item class implement INotifyPropertyChanged and with other UI part it works.
With TreeView the first bind works fine. All node appear with the right data (an image and 2 labels).
But if I change a value (Text) of a Item (from antoher part of UI binded on the same Item object) the TreeView don't reflect the changes!
Either the Item belongs a expanded or collapsed node.
Meanwhile another UI part (Label) that is binded to the same prop of the same Item class, changes!?
Note:
in Debug, in the Output Window i saw this strange warnings, that disappear if I remove the binding in the ItemTemplate:
[0:] Binding: 'StructID' property not found on 'X4DNotes.Views.ItemsPage', target property: 'Xamarin.Forms.Label.Text'
[0:] Binding: 'Payload' property not found on 'X4DNotes.Views.ItemsPage', target property: 'Xamarin.Forms.Image.Source'
[0:] Binding: 'Text' property not found on 'X4DNotes.Views.ItemsPage', target property: 'Xamarin.Forms.Label.Text'
X4DNotes.Views.ItemsPage is the ContentPage that contain the TreeView ... but the binding is ONLY in the ItemTemplate, see next XAML.
And initial binding works on ItemTemplate!!???
DAMN!
while writing this .... I solved, I leave this thread in case someone should encounter the same problem
The problem was the implementation of INotifyPropertyChanged !
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null) return;
IsChanged = true;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
and calling it according to docs:
public string Text { get => _Text; set { if (_Text != value) { _Text = value; OnPropertyChanged(); } } }
but with that code I got the issue discussed in this thread.
since I am a craftsman and very pragmatic I have tried this:
public string Text { get => _Text; set { if (_Text != value) { _Text = value; OnPropertyChanged("Text"); } } }
and with this code all WORKS FINE!!!
Thanks for attention.
PS: Syncfusion guys ... do think do you share this issue with MS?
Davide
XAML:
<navigationdrawer:SfNavigationDrawer.DrawerContentView>
<sf:SfTreeView x:Name="treeView"
ItemsSource="{Binding Items}"
ChildPropertyName="Childs"
SelectionMode="Single"
SelectionChanging="TreeView_SelectionChanging"
SelectionChanged="TreeView_SelectionChanged"
NotificationSubscriptionMode="CollectionChange"
>
<sf:SfTreeView.ItemTemplate>
<DataTemplate>
<Grid Padding="2,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding StructID}" HorizontalTextAlignment="Start" FontAttributes="Bold"/>
<Image Source="{Binding Payload,Converter={StaticResource PayLoadToImage}}" WidthRequest="48"
IsVisible="{Binding . , Converter={StaticResource ItemIsImage}}" Margin="4,0,0,0"/>
<Label Text="{Binding Text}" Margin="2,0,0,0"/>
</StackLayout>
</Grid>
</DataTemplate>
</sf:SfTreeView.ItemTemplate>
</sf:SfTreeView>
</navigationdrawer:SfNavigationDrawer.DrawerContentView>
</navigationdrawer:SfNavigationDrawer>