Reputation: 233
I am a newbie in Xamarin Forms (XF). When I try to bind data, I face a problem that I do not understand. If I update any property by interface (like picker), PropertyChanged
exist. But if I update any property by code, PropertyChanged
do not exist. I try to fix it by assigning PropertyChanged = delegate {}
, PropertyChanged
exist but handler(this, new PropertyChangedEventArgs(propertyName));
can't update value of property on my interface.
Here is my example:
<Picker Grid.Row="1" Title="Select a component" ItemsSource="{Binding ComponentTree.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedGrandFatherComponent}" />
<Picker Grid.Row="2" Title="Select a component" ItemsSource="{Binding SelectedGrandFatherComponent.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedFatherComponent}" />
<Picker Grid.Row="3" x:Name="SelectedComponentPicker" Title="Select a component" ItemsSource="{Binding SelectedFatherComponent.Children}" ItemDisplayBinding="{Binding Name}" SelectedItem="{Binding SelectedComponent}" />
<ScrollView Grid.Row="4">
<interfaces:SimpleTreeView BindingContext="{Binding SelectedComponent}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ScrollView>
<Label Text="{Binding SelectedComponentStructure}"></Label>
<Button Grid.Column="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Command="{Binding Appear}" BackgroundColor="Transparent" CommandParameter="{Binding Name}" />
public TreeNode()
{
Appear = new Command<string>((x) => ShowUp(x));
}
public static void ShowUp(string name)
{
StructureDesignViewModel instance = new StructureDesignViewModel();
StructureDesignViewModel.HandleSelectedComponent delegateIns = new StructureDesignViewModel.HandleSelectedComponent(instance.SetSelectedComponent);
delegateIns(name);
}
public class StructureDesignViewModel : ObservableObject, INotifyPropertyChanged
{
private static readonly StructureDesignViewModel instance = new StructureDesignViewModel();
public static StructureDesignViewModel Instance
{
get
{
return instance;
}
}
public static IList<Component> Components
{
get
{
return ComponentData.Components;
}
}
TreeNode selectedGrandFatherComponent;
TreeNode selectedFatherComponent;
TreeNode selectedComponent;
string selectedComponentStructure;
public TreeNode BaseTree
{
get; set;
}
public TreeNode ComponentTree
{
get; set;
}
void GrowUp(TreeNode node, List<Component> components)
{
int len = components.Count;
for (int i = 0; i < len; i++)
{
var currentNode = node.Children.Add(new TreeNode { Name = components[i].Name, Status = components[i].Status, IsExpanded = false, Modals = components[i].Modals });
GrowUp((TreeNode)currentNode, components[i].Childs);
}
}
public StructureDesignViewModel()
{
ComponentTree = new SimpleTreeView().ViewModel.MyTree;
ComponentTree = new TreeNode { Name = "Component Root", Status = 0, IsExpanded = true };
GrowUp(ComponentTree, (List<Component>)Components);
}
public TreeNode SelectedGrandFatherComponent
{
get { return selectedGrandFatherComponent; }
set
{
if (selectedGrandFatherComponent != value)
{
selectedGrandFatherComponent = value;
OnPropertyChanged();
}
}
}
public TreeNode SelectedFatherComponent
{
get
{
if (selectedGrandFatherComponent?.Children.Count > 0)
{
selectedFatherComponent = (HMIStudio.Shared.Interfaces.TreeNode)selectedGrandFatherComponent.Children[0];
}
return selectedFatherComponent;
}
set
{
if (selectedFatherComponent != value)
{
selectedFatherComponent = value;
OnPropertyChanged();
}
}
}
public TreeNode SelectedComponent
{
get
{
if (selectedFatherComponent?.Children.Count > 0)
{
selectedComponent = (HMIStudio.Shared.Interfaces.TreeNode)selectedFatherComponent.Children[0];
}
return selectedComponent;
}
set
{
if (selectedComponent != value)
{
selectedComponent = value;
OnPropertyChanged();
}
}
}
public string SelectedComponentStructure
{
get
{
return selectedComponentStructure;
}
set
{
if (selectedComponentStructure != value)
{
Set("SelectedComponentStructure", ref selectedComponentStructure, value);
}
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
if (propertyName == "SelectedGrandFatherComponent")
{
OnPropertyChanged(nameof(SelectedFatherComponent));
}
else if (propertyName == "SelectedFatherComponent")
{
OnPropertyChanged(nameof(SelectedComponent));
}
}
}
public delegate void HandleSelectedComponent(string Name);
public void SetSelectedComponent(string name)
{
Console.WriteLine("Showing selected component {0}", name);
SelectedComponentStructure = name;
}
}
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void Set<T>(string propertyName, ref T backingField, T newValue, Action beforeChange = null, Action afterChange = null)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("propertyName");
if (backingField == null && newValue == null)
return;
if (backingField != null && backingField.Equals(newValue))
return;
if (beforeChange != null)
beforeChange();
backingField = newValue;
OnPropertyChanged(propertyName);
if (afterChange != null)
afterChange();
}
}
My program's flow:
StructureDesignViewModel
get data from ComponentData.Components
(tree structure data) to pass to ItemsSource
in view.GrandFatherComponent
, GrandFatherComponent.Children
is ItemSource
of next picker.SimpleTreeView/SelectedComponent
, I call function Appear
to assign name of node to SelectedComponentStructure
.My problem is SelectedComponentStructure
was changed but interface do not update it.
Thanks for reading! Best regards!
Upvotes: 0
Views: 242
Reputation: 2912
StructureDesignViewModel
public class StructureDesignViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string selectedComponentStructure;
public string SelectedComponentStructure
{
get
{
return selectedComponentStructure;
}
set
{
selectedComponentStructure = value;
SetPropertyValue(ref selectedComponentStructure, value);
}
}
protected void OnPropertyChanges([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetPropertyValue<T>(ref T bakingFiled, T value, [CallerMemberName] string proertyName = null)
{
bakingFiled = value;
OnPropertyChanges(proertyName);
}
}
Try this way..
Upvotes: 2