WPF Binding ObservableCollection to ComboBox - c#

I try to bind a list of Lens Objects and I would like to display the LensName property in my combobox.My lists in my code contain objects but comboboxes remain empty or the property doesn't display.I already tried all the ways known to bind my data without result.Thanks for helping
Xaml
<ComboBox x:Name="RightbestlensCombo" ItemsSource="{Binding Source=RightBestLensList}" DisplayMemberPath="LensName" SelectedValuePath="LensTypeId" />
<ComboBox x:Name="LeftbestlensCombo" ItemsSource="{Binding Source=LefttBestLensList}" DisplayMemberPath="LensName" SelectedValuePath="LensTypeId" ></ComboBox>
Code Behind
public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
if (OphtalboxIA.OphtalboxComputingModule.LeftBestLensList != null)
{
if (OphtalboxIA.OphtalboxComputingModule.LeftBestLensList.Count > 0)
{
LeftBestlensList=new ObservableCollection<OphtalboxIA.Lens>(OphtalboxIA.OphtalboxComputingModule.LeftBestLensList);
//LeftbestlensCombo.ItemsSource = LeftBestlensList;
}
}
if (OphtalboxIA.OphtalboxComputingModule.RightBestLensList != null)
{
if (OphtalboxIA.OphtalboxComputingModule.RightBestLensList.Count > 0)
{
RightBestlensList=new ObservableCollection<OphtalboxIA.Lens>(OphtalboxIA.OphtalboxComputingModule.RightBestLensList);
//RightbestlensCombo.ItemsSource = RightBestlensList;
}
}
My class Lens
[XmlInclude(typeof(Lens))]
public class Lens{
public String LensName;
public String LensType;
public String LensTypeTrial;
public float Diameter;
public float Radius;
public float Sphere;
public float Cylinder;
public int Axis;
public String Addition;
public String Description;
public int isRX;
public int isOphtalBox;
public int priorityOrder;
public int LensFrequencyId;
public string LensFrequencyName;
public int LensTypeId;
public int LensMaterialId;
}

You need properties, not fields. These are fields:
public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
As properties, they would look like this:
private readonly ObservableCollection<OphtalboxIA.Lens> _rightList = new ObservableCollection<OphtalboxIA.Lens>();
private readonly ObservableCollection<OphtalboxIA.Lens> _leftList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> RightBestlensList { get { return _rightList; }}
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList { get { return _leftList; }}
Furthermore, you have a typo in your binding: Source=LefttBestLensList. (One extra "t") And the casing is wrong ("...Lens..." vs. "...lens...").

RightBestlensList and LeftBestlensList must be in ViewModel class and not in Code Behind and they must be a properties.

You have to try below menioned code.
you have to Declare ObservableCollection in your ViewModel Like
private ObservableCollection<Lens> _RightBestLensList = new ObservableCollection<Lens>();
public ObservableCollection<Lens> RightBestLensList
{
get { return _RightBestLensList; }
set { _RightBestLensList = value; RaisePropertyChanged("RightBestLensList"); }
}
Your Lens Class Should be
[XmlInclude(typeof(Lens))]
public class Lens
{
public string LensName { get; set; }
public string LensType { get; set; }
}

Related

UWP C# How to append .json file and to identify them in groups

I am trying to create a usercontrol to display groups of items from json file on windows iot core.
I have a "Create Group" button. Once pressed, it will create 64 usercontrols with respective details and display in a scrollviewer. susequently i can edit individual items in any of the 64 usercontrol then save the json file.
I have my class for the usercontrol as follow;
I have having an issue on how to create different groups of the 64 items and append all in a same json file and to subsequently display them from selection of the mentioned different groups.
Please help thanks.
Group Class
[DataContract]
public class DecoderGroup
{
[DataMember]
public int groupID{ get; set; }
[DataMember]
public string groupName{ get; set; }
[DataMember]
public int cardAddress { get; set; }
[DataMember]
public bool enabled { get; set; }
[DataMember]
public int z1label { get; set; }
[DataMember]
public int z2label { get; set; }
[DataMember]
public int z3label { get; set; }
[DataMember]
public int z4label { get; set; }
[DataMember]
public bool zone1 { get; set; }
[DataMember]
public bool zone2 { get; set; }
[DataMember]
public bool zone3 { get; set; }
[DataMember]
public bool zone4 { get; set; }
[DataMember]
public List<byte> txData { get; set; }
public DecoderGroup(int id, int address, int z1, int z2, int z3, int z4)
{
groupName = "Group";
zone1 = false;
zone2 = false;
zone3 = false;
zone4 = false;
z1label = z1;
z2label = z2;
z3label = z3;
z4label = z4;
}
}
MainPage.xaml.cs
private void AddGroup_Click(object sender, RoutedEventArgs e)
{
ZonesList_Panel.Children.Clear();
int groupid = 1;
int cardadr;
for (cardadr = 1; cardadr <= MAXCARDS; cardadr++)
{
var z4 = (4 * cardadr);
var z3 = (4 * cardadr) - 1;
var z2 = (4 * cardadr) - 2;
var z1 = (4 * cardadr) - 3;
DecoderGroupUserControl decoderGroupControl = new DecoderGroupUserControl(this, new DecoderGroup(groupid, cardadr, z1, z2, z3, z4));
ZonesList_Panel.Children.Add(decoderGroupControl);
}
}
private async void SaveGroup_Click(object sender, RoutedEventArgs e)
{
await saveGroupsToJSON(getGroups());
}
public async Task saveGroupsToJSON(List<DecoderGroup> groups)
{
var serializer = new DataContractJsonSerializer(typeof(List<DecoderGroup>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(DECODERGROUPS_FILE, CreationCollisionOption.OpenIfExists))
{
serializer.WriteObject(stream, groups);
}
}
public List<DecoderGroup> getGroups()
{
List<DecoderGroup> ret = new List<DecoderGroup>();
foreach (DecoderGroupUserControl u in ZonesList_Panel.Children)
{
//add condition for group ID
ret.Add(u.decoderGroup);
}
return ret;
}
UserControl
public DecoderGroupUserControl(MainPage page, DecoderGroup group)
{
this.InitializeComponent();
mainPage = page;
this.decoderGroup = group;
Z1Name.Text = group.z1label.ToString();
Z2Name.Text = group.z2label.ToString();
Z3Name.Text = group.z3label.ToString();
Z4Name.Text = group.z4label.ToString();
}
It is recommended to use UserControl as the DateTemplate of ListView so that you don’t need to create multiple usercontrol and add them to the page. Then you could read json file and convert json objects to a collection, and you could use the collection as the Itemsource of ListView.
By implementing the INotifyPropertyChanged interface, TwoWay data binding could reflect the UI changes to the collection. Finally, you could write the changed collection to the json file.
Note that you need to download the Newtonsoft.Json to parse the json object via Manage NuGet Packages. Please refer to the following code.
MyUserControl1.xaml:
<UserControl
..>
<Grid>
<!--customize the usercontrol style-->
<StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBox Margin="0,0,20,0" Text="{Binding Name,Mode=TwoWay}" BorderThickness="0"/>
<TextBox Text="{Binding Job,Mode=TwoWay}" BorderThickness="0"/>
</StackPanel>
<TextBox Text="{Binding Age,Mode=TwoWay}" BorderThickness="0"/>
</StackPanel>
</Grid>
</UserControl>
MainPage.xaml:
<Page..>
<Grid>
<StackPanel>
<ListView ItemsSource="{x:Bind Results,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<local:MyUserControl1>
</local:MyUserControl1>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click"/>
</StackPanel>
</Grid>
</Page>
MainPage.xaml.cs:
namespace WriteJson
{
public sealed partial class MainPage : Page
{
public ObservableCollection<Person> Persons { get; set; }
public ObservableCollection<Person> Results { get; set; }
public string path;
public MainPage()
{
this.InitializeComponent();
CreateJsonFile();
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
Results = JsonConvert.DeserializeObject<ObservableCollection<Person>>(File.ReadAllText(path));
Debug.WriteLine("bind successfully");
}
public async void CreateJsonFile()
{
//check if info.json exists, if it doesn't exist, create it
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file;
try
{
file = await folder.GetFileAsync("info.json");
}
catch
{
await folder.CreateFileAsync("info.json");
Persons = new ObservableCollection<Person>()
{
new Person(){Name="tom",Job="teacher",Age=24},
new Person(){Name="lily",Job="nurse",Age=20},
new Person(){Name="ming",Job="student",Age=26},
new Person(){Name="kiki",Job="lawyer",Age=28},
new Person(){Name="jack",Job="worker",Age=21},
};
path = ApplicationData.Current.LocalFolder.Path + "\\info.json";
File.WriteAllText(path, JsonConvert.SerializeObject(Persons));
Debug.WriteLine("create a json file successfully");
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText(path, JsonConvert.SerializeObject(Results));
Debug.WriteLine("save successfully");
}
}
public class Person:INotifyPropertyChanged
{
private string name;
private string job;
private int age;
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged("Name");
}
}
public string Job
{
get { return job; }
set
{
job = value;
RaisePropertyChanged("Job");
}
}
public int Age
{
get { return age; }
set
{
age = value;
RaisePropertyChanged("Age");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyname=null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
}

binding a list of class to a data grid UWP

I would like to display this object(Node) in a data grid (coordinate is a custom class that literally describes a coordinate) -
public class Node
{
[JsonRequired]
private bool finished;
[JsonRequired]
private readonly string type;
[JsonRequired]
private coordinate starting_point = null;
[JsonRequired]
private string ID = "";
[JsonRequired]
public coordinate Final_Dest = null;
[JsonRequired]
public List<coordinate> check_points = new List<coordinate>();
[JsonRequired]
private string Metadata = "";
[JsonRequired]
private readonly SimpleMarkerSymbol symbol;
[JsonRequired]
private int Vmax;
[JsonRequired]
public int Amax;
[JsonRequired]
private int slope_max;
[JsonRequired]
private int slope_min;
[JsonRequired]
private int terrein_rank_reject;
[JsonRequired]
private int affinity_to_stay_in_group;
[JsonRequired]
public int GroupID;
[JsonRequired]
public bool coomplitionflag = false;
this is what I tried to do -
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
selected_nodes = (comboclass)e.Parameter;
postioning_index = selected_nodes.Get_index();
Node[] array_of_nodes = selected_nodes.Get_node_array();
dataGrid.ItemsSource = Create_list(array_of_nodes);//this function creates a list of nodes
}
public static List<Node> Create_list(Node[] array_nodes)
{
var node_coll = new List<Node>();
for (int i = 0; i < array_nodes.Length; i++)
{
node_coll.Add(array_nodes[i]);
}
return node_coll;
}
and for some reason when I execute it only shows the field -type on the data grid
binding a list of class to a data grid UWP
If you want to bind the Node instance, you need implement the public field's set get method.
Please edit your class like the following.
public class Node
{
public string Title { get; set; }
public coordinate Final_Dest { get; set; }
.......
}
For more detail please refer Data binding in depth document.

How can i bind xml to mvvm wpf?

I have a problem. I have wpf mvvm app and I need to bind xml, but I don't know how.
I have there elements model, elements vm, and a view. Everything work, but all of that elements have "some base" model.
class ItemModel
{
public ItemModel(string name, double weight, double sg, double volume)
{
Name = name;
Weight = weight;
Sg = sg;
Volume = volume;
}
public string Name { get; set; }
public double Weight { get; set; }
public double Sg { get; set; }
public double Volume { get; set; }
}
This is my VM.
class ItemViewModel : BaseVM
{
public ItemViewModel(string name, double sg, double weight, double volume)
{
Name = name;
Weight = weight;
Sg = sg;
Volume = volume;
}
public string Name { get; set; }
private double _weight;
public double Weight
{
get => _weight;
set
{
_weight = value;
RaisePropertyChanged();
}
}
private double _sg;
public double Sg
{
get => _sg;
set
{
_sg = value;
Weight = value * _volume;
RaisePropertyChanged("Weight");
RaisePropertyChanged("Sg");
}
}
private double _volume;
public double Volume
{
get => _volume;
set
{
_volume = value;
_weight = value * _sg;
RaisePropertyChanged();
RaisePropertyChanged("Weight");
RaisePropertyChanged("Sg");
}
}
}
This is my MainVM
class MainViewModel
{
private DataModel Data;
public ObservableCollection<ItemViewModel> Items { get; set; }
public ListCollectionView FilteredItems { get; set; }
public MainViewModel()
{
Data = new DataModel();
Items = new ObservableCollection<ItemViewModel>();
FilteredItems = new ListCollectionView(Items)
{
Filter = item => ((ItemViewModel)item).Volume != 0,
IsLiveFiltering = true,
LiveFilteringProperties =
{
nameof (ItemViewModel.Volume)
}
};
Load();
}
public void Load()
{
foreach (var item in Data.GetItems())
Items.Add(new ItemViewModel(item.Name, item.Weight, item.Sg, item.Volume));
}
}
I have some "DataModel"
class DataModel
{
public List<ItemModel> GetItems() =>
new List<ItemModel>
{
new ItemModel("Water", 0.00, 1.025, 0.00),
new ItemModel("Ballast", 0.00, 1.000, 0.00),
new ItemModel("Oil", 0.00, 1.040, 0.00),
};
}
And this is xml i want to bind instead.
<ballast>
<tank ID="FPTW" Name="Forepeak" Weight="0.00" SG="1.025" Volume="0.00"> </tank>
</ballast>
Please help me how can i bind this xml file instead list in DataModel.
I strongly recommend to deserialize your XML to "real" objects. In your example you want to have a list of tanks in your programm. The view (XAML) should not know if the "data storage" is a xml file, database or whatever. Just bind to a list of TankViewModel (or in your case you named it ItemViewModel. Hopefully you don't blow something up with all these tanks....
This is how you deserialize the xml to "real" objects (full working console app:
https://dotnetfiddle.net/zCHSmc
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
MainViewModel vm = new MainViewModel();
foreach(tank t in vm.ListOfTanks.ballast)
{
Console.WriteLine(t.ID);
}
}
public class MainViewModel
{
public tanks ListOfTanks
{
get
{
string xmlContent = "<?xml version=\"1.0\" encoding=\"utf-16\"?><tanks xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <ballast><tank><ID>ksj</ID></tank> </ballast></tanks>";
tanks allTanks = null;
XmlSerializer serializer = new XmlSerializer(typeof(tanks));
//if you have a xml file you can use serializer.Deserialize("yourFile.xml"); instead of the "in memory stream"
using (TextReader reader = new StringReader(xmlContent))
{
allTanks =(tanks)serializer.Deserialize(reader);
}
return allTanks;
}
}
}
public class tanks
{
public tanks()
{
ballast = new List<tank>();
}
public List<tank> ballast {get;set;}
}
public class tank
{
public string ID {get;set;}
}
}

Change data with buttonclick - MVVM

I have a ListView with an List<List<enum>> property and i have a View that shows each bool as button.
I want to cycle through the bound enum when someone clicks on the button. The problem is I cant use a normal click because it would be outside of my ViewModel.
Edit:
I have the class TruthTable that has 2 DynTable:
public sealed class Column<T>
{
public Column()
{
ColumnData = new List<T>();
ColumnHeader = "";
}
...
public List<T> ColumnData { get; set; }
public string ColumnHeader { get; set; }
}
public sealed class DynTable<T>
{
public DynTable()
{
Columns = new List<Column<T>>();
}
...
public List<Column<T>> Columns { get; set; }
}
public sealed class TruthTable
{
public TruthTable()
{
input = new DynTable<bool>();
results = new DynTable<BoolState>();
}
...
private DynTable<bool> input;
private DynTable<BoolState> results;
public DynTable<bool> Input { get { return input; } set { input = value; } }
public DynTable<BoolState> Results { get { return results; }}
}
public enum BoolState
{
False = 0,
True = 1,
DontCare = 2
}
And i have a ViewModel for the TruthTable. I dont think that the I have to show the code for the ViewModel because its just a TruthTable property. I hope thats enough code to understand my problem ._.

do not work "set" in some of property in design time

why in desin time do not work set "dataGrid" Property in my code.but property font,with working correct.
I used of this componet on the a Form.(i test it with design time debuging)
enter code here
namespace Example
{
public partial class Component1 : System.Windows.Forms.DataGridView
{
public Component1()
{
}
private DataGridViewColumn _gridcolumn;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof ( ExpandableObjectConverter))]
public DataGridViewColumn gridcolumn
{
get
{
if (_gridcolumn == null)
_gridcolumn = new DataGridViewColumn();
return _gridcolumn;
}
set //Do not Work Set in designTime
{
_gridcolumn = value;
}
}
private System.Drawing.Font _MyFont;
public System.Drawing.Font MyFont
{
get
{
if (_MyFont == null)
_MyFont = new System.Drawing.Font("tahoma", 8);
return _MyFont;
}
set
{
_MyFont = value; //Work correctly in design time
}
}
int _with;
public int withcustom
{
get
{
return _with;
}
set
{
_with = value; //Work correctly in design time
}
}
}
}
Short Version
Either remove DesignerSerializationVisibility(DesignerSerializationVisibility.Content) from your property, or change it to DesignerSerializationVisibility(DesignerSerializationVisibility.Visible).
Long Version
Applying DesignerSerializationVisibility(DesignerSerializationVisibility.Content) to your property means that the WinForms designer will not generate code that sets the value of the property, but rather code that sets the value of the properties on the value of that property.
For example, say I have these two types:
public class MyControl : Control
{
public class MyControlProperties
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
private MyControlProperties props;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyControlProperties Properties
{
get
{
if(props == null) props = new MyControlProperties();
return props;
}
}
}
When the designer generates the code for MyControl on a form, it will look something like this:
myControl.Properties.Prop1 = "foo";
myControl.Properties.Prop2 = 10;
So, instead of setting the value of the Properties property (which, in this code, is read-only; though it doesn't have to be, properties like this usually are), it's setting the values of the properties on the value of that property.
This is good example:
public partial class SCon : UserControl
{
public SCon()
{
InitializeComponent();
if (Persoanas == null)
{
Persoanas = new List<Persoana>();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<Persoan> Persoanas { get; set; }
}
[Serializable]
public class Persoan
{
public int Id { get; set; }
public String Name { get; set; }
}

Categories