I am in trouble while working on Databinding in windows form. I have two classes, one is Project and another is Update. Now all project object is having a list of Updates and it is binded to a combobox, but when the user changes selection need to display/bind the properties of Update object to another controls. But is not updating as expected, when the user changes selection. Please help me on this..
Screenshot
See my class and codes below,
public class Project
{
private int _id;
private string _name;
public Project(int id, string name)
{
_id = id;
_name = name;
ReadUpdates();
}
public List<Update> AvailableUpdates { get; set; }
public int Id { get { return _id; } }
public string ProjName
{
get { return _name; }
set { _name = value; }
}
private void ReadUpdates()
{
AvailableUpdates = new List<Update>();
for (int i = 0; i < 10; i++)
{
AvailableUpdates.Add(new
Update(i, DateTime.Now.AddDays(i)));
}
}
}
public class Update
{
private string _title;
private int _uid;
private DateTime _updatedOn;
public Update(int id, DateTime updatedOn)
{
_title = $"Update:{id}";
_uid = id;
_updatedOn = updatedOn;
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public int UId
{
get { return _uid; }
set { _uid = value; }
}
public DateTime UpdatedOn
{
get { return _updatedOn; }
set { _updatedOn = value; }
}
}
public partial class Form1 : Form
{
private Update _currentUpdate;
private Project _project;
public Form1()
{
InitializeComponent();
_project = new Project(1, "Sample Project");
DoBindings();
}
private void DoBindings()
{
NameBox.DataBindings.Add("Text", _project, "ProjName");
IdBox.DataBindings.Add("Text", _project, "Id");
UpdatesCombo.DataSource = _project.AvailableUpdates;
UpdatesCombo.DisplayMember = "UId";
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
UpdateTitle.DataBindings.Add("Text", _currentUpdate, "Title");
UpdateDate.DataBindings.Add("Value", _currentUpdate, "UpdatedOn");
}
private void UpdatesCombo_SelectionChangeCommitted(object sender, System.EventArgs e)
{
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
}
}
Please correct me if I am wrong.
It's quite simple. All you need is to bind the related controls to the same list data source as the combo box.
var updates = _project.AvailableUpdates;
UpdatesCombo.DataSource = updates;
UpdatesCombo.DisplayMember = "UId";
UpdateTitle.DataBindings.Add("Text", updates, "Title");
UpdateDate.DataBindings.Add("Value", updates, "UpdatedOn");
The data binging infrastructure creates CurrencyManager class per each unique list data source. The non list controls are actually bound to the Current property, which is updated by the combo box selection.
Related
I have two listview the other one is consider as details for the first one, however the number of the row in the second listview determined by the first listview. The first listview has 3 columns one the first two are strings and the third one is the number of rows for the second listview, how can i add both of the listview? it sounds like 2D array but in listview.
I can not find a way to link two listviews.
here is the class for both lists:
public class LvwInfo
{
private string _command = "";
private string _saperator = ",";
//
private int _noSeg=0;
public int _NoSeg { get { return _noSeg; } set { _noSeg = value;} }
public string _Command { get { return _command; } set { _command = value; } }
public string _Saperator { get { return _saperator; } set { _saperator = value; } }
public LvwInfo(string cmd,string sp,int Noseg)
{
_command = cmd;
_saperator = sp;
_noSeg = Noseg;
}
}
public class LvwInfoSeg
{
private string _segm = "";
private string _variable = "";
private string _partly = "";
public string _Segm { get { return _segm; } set { _segm = value; } }
public string _Variable { get { return _variable; } set { _variable = value; } }
public string _Partly { get { return _partly; } set { _partly = value; } }
public LvwInfoSeg(string seg, string var, string par)
{
_segm = seg;
_variable = var;
_Partly = par;
}
}
}
and Here how i initilize them:
private List<LvwInfo> _lvwInfo { get; set; }
private List<List<LvwInfoSeg>> _lvwInfoSeg { get; set; }
I want to know how can i Add both list and link them together?
I am working on a C# project which includes WPF. I was wondering, If I could somehow check If my data grid contains certain element.
For example,
I have combo box whose itemsSource is some list of objects. Now, when an user selects an item from the combo box and presses the button
below in data grid (in same window) that item shows up.
I want to forbid the user to select same item more than once and for example put MessageBox with error message. How could I do that?
Code
This Window:
public partial class AvioWindowAddNEdit : Window
{
Avio avio;
public enum Stage { ADD, EDIT};
Stage stage;
public AvioWindowAddNEdit(Avio avio, Stage stage = Stage.ADD)
{
InitializeComponent();
this.avio= avio;
this.stage= stage;
textboxCode.DataContext = avio;
comboboxListofFlights.ItemsSource = Aplikacija.Instance.Flights;
comboboxListofFlights.DataContext = avio;
datagridListofFlights.ItemsSource = avio.ListofFlights;
datagridListofFlights.ColumnWidth = new DataGridLength(1, DataGridLengthUnitType.Auto);
if (stage== Stage.EDIT)
{
textboxCode.IsEnabled = false;
}
}
}
Button which adds selected item to data grid:
private void btnAddFlight_Click(object sender, RoutedEventArgs e)
{
avio.ListOfFlights.Add(comboboxListOfFlights.SelectedItem as Flight);
}
Singleton class for loading in all of my data:
class Aplication
{
public ObservableCollection<User> Users { get; set; }
public ObservableCollection<Airport> Airports { get; set; }
public ObservableCollection<Flight> Flights{ get; set; }
public ObservableCollection<Avio> Avios { get; set; }
public string LoggedInUser { get; set; }
private static Aplication instance = new Aplication();
public static Aplication Instance
{
get
{
return instance;
}
}
private Aplication()
{
Users= new ObservableCollection<User>();
Airports = new ObservableCollection<Airport>();
Flights = new ObservableCollection<Flight>();
Avios= new ObservableCollection<Avio>();
FillInData(); //method where I filled in all of these ObservableCollections
}
}
My class:
public class Avio : ObservableObject, ICloneable
{
//observableobject is an object where I implemented INotifyPropertyChanged
private string code;
public string Code
{
get { return code; }
set { code= value; OnPropertyChanged("Code"); }
}
private ObservableCollection<Flight> listOfFlights;
public ObservableCollection<Flight> ListOfFlights
{
get { return listOfFlights; }
set { listOfFlights= value; OnPropertyChanged("ListOfFlights"); }
}
private bool active;
public bool Active
{
get { return active; }
set { active= value; OnPropertyChanged("Active"); }
}
public Avio()
{
active= true;
ListOfFlights = new ObservableCollection<Flight>();
}
public Avio(string code)
{
active= true;
ListOfFlights = new ObservableCollection<Flight>();
Code= code;
}
}
You could use an ObservableCollection as an ItemsSource for your DataGrid. In that way you'll always have easy access to the data via code.
Check out this tutorial as a starting point (this uses ListBox instead of DataGrid, but it's easily adaptable to DataGrid).
Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.
It is WPF application and I’m trying to bind individual collection item property in TextBlock. I search on StackOverflow and many others have asked similar questions and they have their solution working. I tried to access value same way but somehow, it’s not displaying Index value in my case so posting similar question. Please help me to identify what I'm doing wrong here.
View model
public class SequeanceViewModel
{
public ObservableCollection<Sequence> SequenceList = new ObservableCollection<ViewModel.Sequence>();
public SequeanceViewModel()
{
for (int i = 1; i <= 6; i++)
{
SequenceList.Add(new ViewModel.Sequence() { Index = i, Name = "Name goes here" });
}
}
}
public class Sequence : INotifyPropertyChanged
{
private int index { get; set; }
private bool current { get; set; }
private string name;
public int Index
{
get
{
return index;
}
set
{
index = value;
OnPropertyChanged(new PropertyChangedEventArgs("Index"));
}
}
public bool Current
{
get
{
return current;
}
set
{
current = value;
OnPropertyChanged(new PropertyChangedEventArgs("Current"));
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
Window code
SequeanceViewModel sequeanceViewModel;
public Validation()
{
InitializeComponent();
sequeanceViewModel = new SequeanceViewModel();
this.DataContext = sequeanceViewModel;
}
Binding in xaml
<TextBlock Text="{Binding SequenceList[0].Index, Mode=OneWay}"></TextBlock>
Since you can only bind to public properties, you must define SequenceList as a property and not as a public field:
public ObservableCollection<Sequence> SequenceList { get; } = new ObservableCollection<ViewModel.Sequence>();
You must expose SequenceList as a property instead of a public variable. Otherwise you cannot bind to it.
In my wpf application, I've created two view classes, DayView and CustomView. In DayView, there is a listbox consist of some entries. In listbox, there is stackpanel with 5 textboxes. In my CustomView class, also I've created stackpanel with 5 textboxes. when I enter data into those textboxes of CustomView and click on save button,the new entry should create and data should saved in respective textboxes of DayView.
CustomView class:
namespace TimeSheet
{
/// <summary>
/// Interaction logic for CustomView.xaml
/// </summary>
public partial class CustomView : Window
{
public List<Harvest_Project> projectList { get; set; }
public List<Harvest_Client> clientList { get; set; }
public Harvest_Project selectedProjectid { get; set; }
public Harvest_Client selectedClientid { get; set; }
private string client { get { return ClientText.Text; } set { ClientText.Text=value;} }
private string application { get { return ApplicationText.Text; } set { ApplicationText.Text = value; } }
private string starttime { get { return StartTimeText.Text; } set { StartTimeText.Text = value; } }
private string stoptime { get { return StopTimeText.Text; } set { StopTimeText.Text = value; } }
private string task { get { return TaskText.Text; } set { TaskText.Text = value; } }
private string project { get { return ProjectText.Text; } set { ProjectText.Text = value; } }
public CustomView()
{
InitializeComponent();
this.DataContext = this;
Globals._globalController.setCustomViewWindow(this);
clientList = Globals._globalController.harvestManager._CLIENTLIST;
projectList = Globals._globalController.harvestManager._PROJECTLIST;
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Harvest_TimeSheetEntry)
{
//Harvest_TimeSheetEntry entry = new Harvest_TimeSheetEntry();
//if (!entry.isSynced)
//{
// entry.ClientNameBinding = client;
// entry.ApplicationNameBinding = application;
// entry.StartTimeBinding = starttime;
// entry.StopTimeBinding = stoptime;
// entry.TaskNameBinding = task;
// entry.ProjectNameBinding = project;
//}
Globals._globalController.getDayViewWindow.Show();
this.Hide();
}
}
private void ClientComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem.ToString();
}
private void ProjectComboBoxChanged(object sender, SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem.ToString();
}
}
}
I don't understand, how should i save this data in DayView listbox. Please suggest some ways.!
This is DayView window
[1]: http://i.stack.imgur.com/1Qi0e.png
This is CustomView window!
[2]: http://i.stack.imgur.com/mACxR.png
In my opinion it's more suitable to use an mvvm Framework to make your solution more structured, and then use a Messenger to make communication between your views.