I've posted the same question before but it wasn't clear (and contained too many self-induced errors in attempt to fix the code) so re-posting it with more details.
So I have "MainUiWindow.xaml" file which uses databinding like this:
<ItemsControl x:Name="gridSettingsMonster" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding SettingsMonster}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type core:Setting}">
<Grid x:Name="gridMonster">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Label}" IsEnabled="{Binding Enabled}" ToolTip="{Binding Description}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10 5 10 5" FontWeight="{Binding Fontweight}" ></TextBlock>
<ts:ToggleSwitch x:Name="toggleSwitchMonsterAll" IsEnabled="{Binding Enabled}" Grid.Row="0" Grid.Column="1" Command ="{Binding TriggerAction}" IsChecked="{Binding Value}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5 0 20 2" Foreground="White" UncheckedText="" CheckedText="" UncheckedBorderBrush="#FF333333" CheckedBorderBrush="#FF2D2D30"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
SettingsMonster binding:
SettingsMonster.Add(new Setting(ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible, true, "Monster_1", "Monster Widget", "Show/Hide Monsters Widget", new Command(_ =>
{
ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible = !ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible;
ConfigHelper.Main.Save();
})));
SettingsMonster.Add(new Setting(ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters, ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible, "Monster_2", " Show unchanged monsters", "Automatically hide monsters if they are not damaged", new Command(_ =>
{
ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters = !ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters;
ConfigHelper.Main.Save();
})));
And finally, the Setting class:
public class Setting
{
public bool Value { get; set; }
public bool Enabled { get; set; }
public string Name { get; }
public string Label { get; }
public string Description { get; }
public string Checkbox_visibility { get; }
public string Fontweight { get; }
public List<Setting>SubSettings { get; }
public Command TriggerAction { get; }
public Setting(bool value, bool enabled, string name, string label, string description, Command action = null)
{
Value = value;
Enabled = enabled;
Name = name;
Label = label;
Description = description;
SubSettings = new List<Setting>();
TriggerAction = action;
}
}
Problem:
When I run the build and use the "ToggleSwitch" (it's basically a open-source checkbox) to change the value of "ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible", it unchecks the UI correctly.
I want this checkbox to control the other checkboxes (i.e. "Monster_2") as well, so that when the main one is turned off, set IsEnabled value for the child checkboxes/textblocks to FALSE.
I got to the stage where if I check off the main one, restart the build, then the child checkboxes/textblocks are all set as IsEnabled=False. However, I want the same to happen in real time (i.e. refresh the UI without having to restart).
Any help would be appreciated.
EDIT 1.
So I have attempted implementing the INotifyPropertyChanged in my Settings class which looks like the following:
public class Setting : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _value;
private bool _enabled;
public bool Value
{
get
{
return _value;
}
set
{
if (_value == value)
return;
_value = value;
OnPropertyChanged(nameof(Enabled));
}
}
public bool Enabled
{
get
{
return _enabled;
}
set
{
if (_enabled == value)
return;
_enabled = value;
OnPropertyChanged(nameof(Enabled));
}
}
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public string Name { get; }
public string Label { get; }
public string Description { get; }
public string Checkbox_visibility { get; }
public string Fontweight { get; }
public List<Setting>SubSettings { get; }
public Command TriggerAction { get; }
public Setting(bool value, bool enabled, string name, string label, string description, Command action = null)
{
Value = value;
Enabled = enabled;
Name = name;
Label = label;
Description = description;
SubSettings = new List<Setting>();
TriggerAction = action;
}
But my UI is still not refreshing yet... any help?
My viewmodel was referencing incorrect variable in the first place.
I have implemented INotifyPropertyChange in my Setting object as below, and also added a command to be run when the checkbox is triggered.
public class Setting : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private bool _value;
private bool _enabled;
public bool Value
{
get
{
return _value;
}
set
{
if (_value == value)
return;
_value = value;
OnPropertyChanged(nameof(Value));
}
}
public bool Enabled
{
get
{
return _enabled;
}
set
{
if (_enabled == value)
return;
_enabled = value;
OnPropertyChanged(nameof(Enabled));
}
}
public string Name { get; }
public string Label { get; }
public string Description { get; }
public string Fontweight { get; }
public List<Setting>SubSettings { get; }
public Command TriggerAction { get; }
public Setting(bool _value, bool _enabled, string name, string label, string description, Command action = null)
{
Value = _value;
Enabled = _enabled;
Name = name;
Label = label;
Description = description;
SubSettings = new List<Setting>();
TriggerAction = action;
}
}
Related
I'm trying to use a dynamically created layout using CollectionView to show a series of properties of a class, all based on a list and I want to make it so one of the properties is a Combobox. How do I know what object the ComboBox needs to refer to?
Here is my CollectionView:
<CollectionView x:Name="taskList">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:Task">
<VerticalStackLayout Margin="15">
<Entry Text="{Binding name}" IsReadOnly="True" />
<Entry Text="{Binding departmentsString}" IsReadOnly="True"/>
<HorizontalStackLayout>
<inputs:SfComboBox BackgroundColor="Black" TextColor="Green" DropDownIconColor="Green"/>
<Entry Text="{Binding deadline}" IsReadOnly="True" />
<Entry Text="{Binding author.fullName}" IsReadOnly="True"/>
</HorizontalStackLayout>
<Entry Text="{Binding description}" IsReadOnly="True" />
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
It has its ItemsSource declared like this:
taskList.ItemsSource = tasks;
tasks being:
ObservableCollection<Classes.Task> tasks { get; set; }
Here is the Task class:
public class Task
{
public Task(string name, List<string> departments, Status status, DateOnly deadline, Employee author, string description)
{
this.name = name;
this.departments = departments;
this.status = status;
this.deadline = deadline;
this.author = author;
this.description = description;
}
public string name { get; private set; }
public List<string> departments { get; private set; } = new List<string>();
public string departmentsString
{
get
{
string _ = "";
foreach (var department in departments)
{
_ += department + (department == departments.Last() ? "": ", ");
}
return _;
}
}
public Status status { get; private set; }
public DateOnly deadline { get; private set; }
public Employee? author { get; set; }
public string description { get; private set; }
public List<Employee> employees { get; private set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
if (departments.Contains(employee.department))
{
employees.Add(employee);
}
}
}
How do I make it so I can determine the instance of the class Task depending on which ComboBox I change?
Here is what the UI looks like:
Trying to bind the combobox to the Status property
You can try to set a data list for property ItemsSource of SfComboBox and bind a field to property SelectedItem of SfComboBox.
Suppose you would bind departments to the ItemsSource of SfComboBox, then we need to add a field (e.g. SelectedItem) to bind to property SelectedItem of SfComboBox:
Then we need to implement interface INotifyPropertyChanged for MyTask.cs and add field SelectedItem.(To prevent conflicts with the Task class in my project, I named it MyTask)
//add SelectedItem here
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
The whole code of MyTask
public class MyTask: INotifyPropertyChanged
{
public MyTask(string name, List<string> departments, int status, DateTime deadline, Employee author, string description)
{
this.name = name;
this.departments = departments;
this.status = status;
this.deadline = deadline;
this.author = author;
this.description = description;
}
//add SelectedItem here
private string _selectedItem;
public string SelectedItem
{
get => _selectedItem;
set => SetProperty(ref _selectedItem, value);
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
public string name { get; set; }
public List<string> departments { get; private set; } = new List<string>();
public string departmentsString
{
get
{
string _ = "";
foreach (var department in departments)
{
_ += department + (department == departments.Last() ? "" : ", ");
}
return _;
}
}
public int status { get; private set; }
public DateTime deadline { get; private set; }
public Employee? author { get; set; }
public string description { get; private set; }
public List<Employee> employees { get; private set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
if (departments.Contains(employee.department))
{
employees.Add(employee);
}
}
}
Then we can use like this:
<editors:SfComboBox BackgroundColor="Black" TextColor="Green"
DropDownIconColor="Green"
WidthRequest="250"
ItemsSource="{Binding departments}"
SelectedItem="{Binding SelectedItem}"
/>
Note:
Then if we change the option of SfComboBox , the value of SelectedItem will also update automatically.
I'm trying to create a UWP-app in C# that can control my lights in my home. I am able to fetch the data from the server and create lamp objects for each individual lamp. These lamp objects are then place in an ObservableCollection on the beginning of the app. This ObservableCollection is bound to a GridView with an DataTemplate. When the app started i can see my lights with the right data. I then refetch the data to check if any lamp property has changed every 500ms. I can clearly see that the object properties are succesfully updated, but the bound data doesn't recognize this change. So the UI does not change either. I tried to use the NotifyPropertyChange in Lamp class, but that did nothing either.
After a lot of trial and error i found that the ui only changes when I add, delete or replace an object in the ObservableCollection, but replacing is not really a practical option for me as it causes a lot of instabillity and does not look like that is the way this problem has to be resolved.
<GridView ItemsSource="{x:Bind LampCollection}" Margin="10 0" HorizontalAlignment="Center">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Lamp">
<Border BorderBrush="#555555" BorderThickness="1" CornerRadius="8" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" >
<Grid Width="300" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{x:Bind ImageUri, Mode=OneWay}" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" >
<TextBlock Name="txt" VerticalAlignment="Bottom" FontSize="20" FontWeight="Bold" Margin="10,0,0,20" Text="{x:Bind Name, Mode=OneTime}"/>
<TextBlock Name="status" VerticalAlignment="Bottom" FontSize="11" FontWeight="Bold" Margin="10,0,0,20" Text="{x:Bind Status, Mode=OneWay}"/>
</StackPanel>
<Rectangle Grid.Row="1" Grid.Column="0" Visibility="{x:Bind ColorLamp}" Width="50" Height="50" Fill="Maroon"/>
<Slider Visibility="{x:Bind Dimmable}" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="10,0,10,0" Value="{x:Bind Brightness, Mode=TwoWay}"/>
</Grid>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Xaml code
The lamp.SetStatus function just parses the string and sets the properties Brightness and Status which are bound to the UI.
foreach (Lamp lamp in LampCollection) {
string response = await GetAsync(UrlString + lamp.IDX.ToString());
dynamic json = JsonConvert.DeserializeObject(response);
if (json.status == "OK") {
lamp.SetStatus(json.result[0].Status.ToString());
}
}
C# update code
Edit
I tried to implement the INotifyPropertyChanged in my lamp class as described in Microsoft's documentation. It doesn't seem to do anything however. I also tried passing in the name in the NotifyPropertyChanged() function but that only made my app crash.
class Lamp : INotifyPropertyChanged {
public uint IDX { get; internal set; }
public string Name { get; internal set; }
public bool Status { get; internal set; }
public string ImageUri { get; internal set; }
public bool Dimmable { get; internal set; }
public bool ColorLamp { get; internal set; }
public uint Brightness { get; set; }
public float[] Color { get; set; }
public Lamp(uint idx, string name, string status, bool dimmable, bool colorLamp) {
IDX = idx;
Name = name;
Color = new float[3];
Dimmable = dimmable;
ColorLamp = colorLamp;
if (status == "Off") {
ImageUri = "Images/lamp-off.svg";
Status = false;
} else {
ImageUri = "Images/lamp-on.svg";
Status = true;
if(dimmable) {
Brightness = uint.Parse(Regex.Match(status, #"\d+").Value, NumberFormatInfo.InvariantInfo);
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Switch(bool status) {
Status = status;
if(status) ImageUri = "Images/lamp-on.svg";
else ImageUri = "Images/lamp-off.svg";
NotifyPropertyChanged();
}
public void SetColor(float r, float g, float b) { if (ColorLamp) { Color[0] = r; Color[1] = g; Color[2] = b; } }
public void SetStatus(string status) {
if (status == "Off") {
if (Status) {
ImageUri = "Images/lamp-off.svg";
Status = false;
if (Dimmable) Brightness = 0;
Debug.WriteLine(Name + "(" + IDX + ") has turned off");
NotifyPropertyChanged();
}
} else {
if (Dimmable) {
uint _tmpBright = uint.Parse(Regex.Match(status, #"\d+").Value, NumberFormatInfo.InvariantInfo);
if(!Status || Brightness != _tmpBright) {
ImageUri = "Images/lamp-on.svg";
Status = true;
Brightness = _tmpBright;
Debug.WriteLine(Name + "(" + IDX + ") has turned on or changed brighntess");
NotifyPropertyChanged();
}
} else {
if (!Status) {
ImageUri = "Images/lamp-on.svg";
Status = true;
Debug.WriteLine(Name + "(" + IDX + ") has turned on");
NotifyPropertyChanged();
}
}
}
}
}
Based your code snippet, you called the NotifyPropertyChanged() method in the SetStatus() method and the CallerMemberName allows you to obtain the method or property name of the caller to the method, if you do not pass any propertyName to the NotifyPropertyChanged() method, it will automatically obtain the method name which is SetStatus. However, there is no UI bound with SetStatus, so the UI won't update. If you want to update the UI which bound with Status and Brightness properties in this scenario, you could pass these two property names to NotifyPropertyChanged() method, for example:
public void SetStatus(string status)
{
if (status == "Off")
{
if (Status)
{
ImageUri = "Assets/2.jpg";
Status = false;
if (Dimmable) Brightness = 0;
Debug.WriteLine(Name + "(" + IDX + ") has turned off");
NotifyPropertyChanged("Status");
NotifyPropertyChanged("Brightness");
}
}
......
}
However, every time when you change the values of Status and Brightness properties in SetStatus() method or other methods in your Lamp class, you need to call NotifyPropertyChanged("xxx") method, it is a little complicated. You can declare a private variable and override get and set methods, in the set method, calling the NotifyPropertyChanged() method, every time set a new value to your property, it will enter the set method and then notify the UI to update. Take Status and Brightness as examples:
public class Lamp : INotifyPropertyChanged
{
private bool status { get; set; }
private uint brightness { get; set; }
public bool Status {
get {
return status;
}
set {
status = value;
NotifyPropertyChanged();
}
}
public uint Brightness
{
get
{
return brightness;
}
set
{
brightness = value;
NotifyPropertyChanged();
}
}
// The same behavior to the following properties
public uint IDX { get; internal set; }
public string Name { get; internal set; }
public string ImageUri { get; internal set; }
public bool Dimmable { get; internal set; }
public bool ColorLamp { get; internal set; }
public float[] Color { get; set; }
public Lamp(uint idx, string name, string status, bool dimmable, bool colorLamp)
{
IDX = idx;
Name = name;
Color = new float[3];
Dimmable = dimmable;
ColorLamp = colorLamp;
if (status == "Off")
{
ImageUri = "Assets/2.jpg";
Status = false;
}
else
{
ImageUri = "Assets/3.jpg";
Status = true;
if (dimmable)
{
Brightness = uint.Parse(Regex.Match(status, #"\d+").Value, NumberFormatInfo.InvariantInfo);
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Switch(bool status)
{
Status = status;
if (status) ImageUri = "Assets/3.jpg";
else ImageUri = "Assets/2.jpg";
}
public void SetColor(float r, float g, float b) { if (ColorLamp) { Color[0] = r; Color[1] = g; Color[2] = b; } }
public void SetStatus(string status)
{
if (status == "Off")
{
if (Status)
{
ImageUri = "Assets/2.jpg";
Status = false;
if (Dimmable) Brightness = 0;
}
}
else
{
if (Dimmable)
{
uint _tmpBright = 30;
if (!Status || Brightness != _tmpBright)
{
ImageUri = "Assets/3.jpg";
Status = true;
Brightness = _tmpBright;
}
}
else
{
if (!Status)
{
ImageUri = "Assets/3.jpg";
Status = true;
}
}
}
}
}
I have a little problem with a ListBox and his binding.
All is good except when I call the function LstExtensionUnSelectAll() because
nothing changed, the checkbox are again checked.
I think it's a stupid thing but I don't see it.
<ListBox ItemsSource="{Binding LstExtension, Mode=TwoWay}" Grid.Row="0">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Extension}" IsChecked="{Binding Checked}"/>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here it's the object LstExtension :
public class CustomExtensions
{
public string Extension { get; set; }
public bool Checked { get; set; }
public CustomExtensions(string ext)
{
Extension = ext;
Checked = true;
}
}
private List<CustomExtensions> _LstExtension;
public IEnumerable<CustomExtensions> LstExtension
{
get { return _LstExtension; }
set
{
if (value != _LstExtension)
{
_LstExtension = value.ToList();
NotifyPropertyChanged("LstExtension");
}
}
}
internal void LstExtensionUnSelectAll()
{
_LstExtension?.ForEach(c => c.Checked = false);
NotifyPropertyChanged("LstExtension");
}
you need to update your CustomExtensions class to use INotifyPropertyChanged so that Checked raises the event whenever the value changes.
public class CustomExtensions : INotifyPropertyChanged
{
public string Extension { get; set; }
private bool _checked;
public bool Checked
{
get { return _checked; }
set
{
if (_checked == value) return;
_checked = value;
RaisePropertyChanged("Checked");
}
}
public CustomExtensions(string ext)
{
Extension = ext;
Checked = true;
}
public virtual event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
I have to change the value in a text box dynamically, on selecting a value from a combox box, which is present in different view. when changing the dependency property's source, the propertychangedEventHandler value is not changing, i.e it is remaining as null, so the event is not getting fired. As a result the text in the textbox is not changing. Below is the code. I have bound the text in textbox to _name property.
public partial class Details : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string name = "";
public Details()
{
InitializeComponent();
Name = Connector.Name;
DataContext = this;
}
public string Name
{
get { return name; }
set
{
name = value; OnPropertyChanged("Name");
}
}
protected void OnPropertyChanged(string s)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(s));
}
}
}
Xaml code
<StackPanel Orientation="Vertical">
<TextBlock Text="Student Details" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"> </TextBlock>
<StackPanel Margin="0,5" Orientation="Horizontal" >
<Label MinWidth="100" MaxWidth="110">Name:</Label>
<Border BorderBrush="Gray" BorderThickness="2">
<TextBox Name="nametextbox" Text="{Binding Name,Mode=TwoWay}" Width="auto" MinWidth="100" FontWeight="Black"></TextBox>
</Border>
</StackPanel>
Is it possible that you accidentally exchanged name and _name, using name in XAML for the binding?
Usually you have a public property with a capitalized name, and a private field with a non-capitalized name, optionally prefixed with an underscore as you did.
So, you should have
public string Name {
get { return _name; }
set { _name = value; OnPropertyChanged("Name"); }
{
private string _name = "";
Please check the following:
If you're not currently binding to name instead of _name;
Either if that is or is not the case, please fix your naming convention, because it is a source of errors, and every example you'll find follow the convention I included above.
In your XAML, you are binding "Name" property and in your code, you have created _name property. So, you need to change it to "Name" property in your code.
Just change your property as per below:
private string _name = "";
public string Name
{
get { return _name; }
set {
_name = value;
OnPropertyChanged("Name");
}
}
Try this and let me know.
I have used eventaggregator for this purpose, as we need to change the text in the text box dynamically when an event in a different view is fired. Below is the C# code of both the DropView(where we select student name from a list), and DetailsView(where we display the details). I publish events in Drop.xaml.cs and subscribe to those events in Details.xaml.cs
Drop.xaml.cs
public partial class Drop : UserControl
{
private IEventAggregator iEventAggregator;
public Drop(IEventAggregator ieventaggregator)
{
InitializeComponent();
iEventAggregator = ieventaggregator;
this.DataContext = this;
var doc = XDocument.Load("C:\\Users\\srinivasaarudra.k\\Desktop\\students.xml");
var names = doc.Descendants("Name");
foreach (var item in names)
{
droplist.Items.Add(item.Value);
}
}
public string name;
public string Naam
{
get { return name; }
set { name = value;
iEventAggregator.GetEvent<Itemselectedevent>().Publish(Naam);
}
}
public string grade;
public string Grade
{
get { return grade; }
set
{
grade = value;
iEventAggregator.GetEvent<gradeevent>().Publish(Grade);
}
}
public string dept;
public string Dept
{
get { return dept; }
set
{
dept = value;
iEventAggregator.GetEvent<deptevent>().Publish(Dept);
}
}
public static string str;
public static string Str
{
get { return str; }
set {
str = value;
}
}
private void droplist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var sel = droplist.SelectedValue;
Str=sel.ToString();
XmlDocument doc2 = new XmlDocument();
doc2.Load("C:\\Users\\srinivasaarudra.k\\Desktop\\students.xml");
var details = doc2.DocumentElement.SelectNodes("/Students/StudentDetails");
foreach (XmlNode node in details)
{
if (node.SelectSingleNode("Name").InnerText == Str)
{
Naam = node.SelectSingleNode("Name").InnerText;
Grade = node.SelectSingleNode("Grade").InnerText;
Dept = node.SelectSingleNode("Department").InnerText;
}
}
// Details det = new Details();
Details dt = new Details(iEventAggregator);
}
}
public class Itemselectedevent:Prism.Events.PubSubEvent<string>
{
}
public class gradeevent : Prism.Events.PubSubEvent<string>
{
}
public class deptevent : Prism.Events.PubSubEvent<string>
{
}
Details.xaml.cs
public partial class Details : UserControl,INotifyPropertyChanged
{
public IEventAggregator iEventAggregator;
public event PropertyChangedEventHandler PropertyChanged;
public static string name;
public static string dept;
public static string grade;
[Bindable(true)]
public string Naam
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Naam");
}
}
[Bindable(true)]
public string Grade
{
get { return grade; }
set
{
grade = value; OnPropertyChanged("Grade");
}
}
[Bindable(true)]
public string Dept
{
get { return dept; }
set
{
dept = value;
OnPropertyChanged("Dept");
}
}
public Details(IEventAggregator eventaggregator)
{
InitializeComponent();
this.iEventAggregator = eventaggregator;
iEventAggregator.GetEvent<Itemselectedevent>().Subscribe((str) => { Naam = str; });
iEventAggregator.GetEvent<gradeevent>().Subscribe((str) => { Grade = str; });
iEventAggregator.GetEvent<deptevent>().Subscribe((str) => { Dept = str; });
this.DataContext = this;
}
protected void OnPropertyChanged(string s)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(s));
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
I want to save the values that a user enters in the placeholder of an EntryCell using MVVM.
This is part of my .xaml
<TableView>
<TableView.Root>
<TableSection>
<EntryCell x:Name="HomeEC"
Label="HomeTeam"
Placeholder="{Binding Home, Mode=TwoWay}"
>
</EntryCell>
<EntryCell x:Name="AwayEC"
Label="AwayTeam"
Placeholder="{Binding Away, Mode=TwoWay}"
>
</EntryCell>
<EntryCell x:Name="BetEC"
Label="BetTeam"
Placeholder="{Binding Bet, Mode=TwoWay}"
>
</EntryCell>
<EntryCell x:Name="TypeEC"
Label="BetType"
Placeholder="{Binding Type, Mode=TwoWay}"
>
</EntryCell>
<EntryCell x:Name="OddEC"
Label="Odd"
Placeholder="{Binding Odd, Mode=TwoWay}"
>
</EntryCell>
</TableSection>
</TableView.Root>
</TableView>
And this is my ViewModel class
public string Home
{
set
{
home = value;
OnPropertyChanged("Home");
newMatch.HomeTeam = home;
}
}
public string Away
{
set
{
away = value;
OnPropertyChanged("Away");
newMatch.AwayTeam = away;
}
}
public string Bet
{
set
{
bet = value;
OnPropertyChanged("Bet");
newMatch.Bet = bet;
}
}
public string Type
{
set
{
type = value;
OnPropertyChanged("Type");
newMatch.BetType = type;
}
}
public string Odd
{
set
{
odd = value;
OnPropertyChanged("Odd");
newMatch.Odd = Decimal.Parse(odd);
}
}
public ICommand InsertBet;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
When I enter my values in the field in the UI, they do not get saved here in the VM. What am I doing wrong?
Thanks,
Dragos
InsertMatchVM.cs
public class InsertMatchVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string home, away, bet;
public Match newMatch = new Match();
public string Home
{
set
{
home = value;
OnPropertyChanged("Home");
newMatch.HomeTeam = home;
}
get
{
return home;
}
}
public string Away
{
set
{
away = value;
OnPropertyChanged("Away");
newMatch.AwayTeam = away;
}
get
{
return away;
}
}
public string Bet
{
set
{
bet = value;
OnPropertyChanged("Bet");
newMatch.Bet = bet;
}
get
{
return bet;
}
}
public ICommand InsertBet;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
Page1.Xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App2;assembly=App2"
x:Class="App2.Page1">
<ContentPage.BindingContext>
<local:InsertMatchVM/>
</ContentPage.BindingContext>
<TableView>
<TableView.Root>
<TableSection>
<EntryCell x:Name="HomeEC"
Label="HomeTeam"
Text="{Binding Home, Mode=TwoWay}"
Placeholder="Home"
>
</EntryCell>
<EntryCell x:Name="AwayEC"
Label="AwayTeam"
Text="{Binding Away, Mode=TwoWay}"
Placeholder="Away"
>
</EntryCell>
<EntryCell x:Name="BetEC"
Label="BetTeam"
Text="{Binding Bet, Mode=TwoWay}"
Placeholder="Bet"
>
</EntryCell>
</TableSection>
</TableView.Root>
</TableView>
</ContentPage>
App.cs
public class App : Application
{
public App()
{
MainPage = new Page1();
}
}
Match.cs
public class Match
{
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
public string Bet { get; set; }
}