Treeview is not inserting or showing data - c#

I am using MonoDevelop to create a GUI but it doesn't contain a ListBox conrol. I read that Tree View is a good alternative so I'm trying to get it to work, but nothing appears to be added to the Tree View at all.
ListStore _store;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
_store = new Gtk.ListStore (typeof(string));
lbErrors.Model = _store; // lbErrors is the Tree View
Error("err");
}
public void Error(string err)
{
var ii = _store.Append();
_store.SetValues(ii, err);
}
Can anyone spot the problem?

You need to set your table columns.
Try something like this:
var column = new TreeViewColumn ();
column.Title = "Column Name";
column.Clickable = false;
var renderer = new CellRendererText ();
column.PackStart (renderer, true);
column.AddAttribute (renderer, "text", 0);
lbErrors.AppendColumn (column);

Related

Creating view with json (Xamarin.Forms)

I'm facing several issues.
I want to deserialize my json to create Views.
I want to create view, so I created a json model who describe my page with several views in it. I have created classes who's going to create my views. Before the json I was doing this in static:
ElementModel element = new ElementModel();
element.Width = 100;
element.Height = 40;
element.Top = 0;
element.Left = 0;
element.Label = "Element num 1";
element.IsEnabled = true;
element.IsMandatory = false;
ElementView ev1 = new TextBoxView(element);
al.Children.Add(ev3.getView());
this.Content = new ScrollView { Content = al };
And it worked fine, this code above was creating an Entry view placed with top, left informations and the width, height dimensions in the json.
I wanted something more dynamic so I created a json, I deserialize it and after that I create my views.
ScrollView sv;
RelativeLayout rl;
AbsoluteLayout al;
public MainPage()
{
InitializeComponent();
al = new AbsoluteLayout();
testDeserialize();
//I was creating view with this function before
//testView();
}
public void testDeserialize()
{
string json = #"myjson in the link";
string newjson = json.Replace("'", "\'");
TourApplication ta = JsonConvert.DeserializeObject<TourApplication>(newjson);
models.Page p = ta.Pages.ElementAt(0);
ElementView ev=null;
foreach (ElementModel em in p.Elments)
{
//ev = new TextBoxView(em);
//al.Children.Add(ev.getView()); doesn't work
Console.WriteLine("-----------------------"+em.Label);
}
this.Content = new ScrollView { Content = al };
}
The creation doesn't work but I'm doing the same operation when it was working.
I have no idea what I can do to fix this.
You can find in this link the json with the generated class structure.
Thx for your help.
EDIT : The problem was due to my json, now I can have my Views. But only one of my View is editable. I mean for the others the keyboard doesn't show.

Update WPF with externalevent and MVVM schema

I'm currently developping a new soft which analyze a lot of data on Revit.
I have my WPF View, my ViewModel and a long methode.
init.cs:
IExternalEventHandler handlerEvent = new FamilyAnalysis();
var familyAnalysisEvent = ExternalEvent.Create(handlerEvent);
Gui = new AnalysisView(familyAnalysisEvent);
Gui.Show();
AnalysisView.xaml.cs:
public AnalysisView(ExternalEvent externalEvent)
{
InitializeComponent();
Windows.SetLocation(this);
Language = System.Windows.Markup.XmlLanguage.GetLanguage(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
DataContext = new AnalysisViewModel(externalEvent, this);
}
AnalysisViewModel.cs:
public ICommand StartAnalysisCommand
{
get
{
return _startAnalysisCommand ?? (_startAnalysisCommand = new RelayCommand(() =>
{
//Some check before then..
_externalEvent.Raise();
}));
}
}
during the _externalEvent.Raise() I would like to have a loading form updating the state of the methode.
I tried to search about dispatcher but I'm not sure how to do that and all my try didn't worked. The loading screen pop but the value are not getting updated.
Any advice ?

Binding a HTML control into a stacklayout using xamarin forms

Being a newbie to Xamrin I am struggling to adding some HTML to a StackLayout via Xamarin Forms. I have tried quite a few things and had a Google around.
Firstly I can't work out which bindable object I am supposed to be using. As I cannot find a straight answer on Google/Xamarin I am going to assume this is not as easy I was hoping.
var nameEntry = new Label ();
nameEntry.SetBinding (Label.TextProperty, "Club.ClubName");
var webView = new WebView ();
webView.SetBinding ( ??? , "Club.Description");
var content = new StackLayout {
Children = {
nameEntry,
???
}
};
I am not sure if this is possible within Xamarin forms itself. Can anyone help?
I should point out my data for the form is being retrieved asynchronously on a remote json endpoint
protected override void OnAppearing ()
{
base.OnAppearing ();
if (ViewModel == null || ViewModel.IsLoading)
return;
ViewModel.LoadItemsCommand.Execute (Club.ClubId);
}
My remote json api contains, Description contatins a HTML snippet which I would like to use.
{
ClubName: "Stourbridge",
Description: "<p>This club meets every TWO weeks on a <b>Friday</b>.</p>"
...
}
Try the following example that will show how to do the bindings.
Note that you have to use a HtmlWebViewSource to achieve this, and bind the WebView.Source to this.
Clicking the button will change the view model and update the WebView appropriately to the newly changed text.
StackLayout objStackLayout = new StackLayout();
MyView objMyView = new MyView();
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text</p></body></html>";
HtmlWebViewSource objHtmlWebViewSource = new HtmlWebViewSource();
objHtmlWebViewSource.SetBinding(HtmlWebViewSource.HtmlProperty, "MyHtml");
objHtmlWebViewSource.BindingContext = objMyView;
WebView objWebview = new WebView();
objWebview.HorizontalOptions = LayoutOptions.FillAndExpand;
objWebview.VerticalOptions = LayoutOptions.FillAndExpand;
objWebview.Source = objHtmlWebViewSource;
Button objMyButton2 = new Button();
objMyButton2.Text="Change Html";
objMyButton2.Clicked+=((o2,e2)=>
{
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text that has changed.</p></body></html>";
});
objStackLayout.Children.Add(objMyButton2);
objStackLayout.Children.Add(objWebview);
The view model is just a simple one with a bindable property as below:-
public class MyView
: Xamarin.Forms.View
{
public static readonly BindableProperty MyHtmlProperty = BindableProperty.Create<MyView, string>(p => p.MyHtml, default(string));
public string MyHtml
{
get { return (string)GetValue(MyHtmlProperty); }
set { SetValue(MyHtmlProperty, value); }
}
}
Before clicking the button gives:-
After clicking the button, adjusts the view model, and automatically updates the control via the binding giving:-

Problems with Syncfusion GridTreeControl and Binding

I'm new to WPF and am using the Syncfusion Framework. I want to use the DataTreeControl to display a hierarchy of data which will be loaded and updated in a reoccuring interval. But for some reason it doesn't display the data.
Here's a snipped from my MainWindow.xaml
<syncfusion:TabItemExt Name="_tabItemTipps" Header="Tipps">
<syncfusion:GridTreeControl Name="_treeGrid"
BorderBrush="LightGray"
BorderThickness="0,0.5,0,0"
EnableHotRowMarker="False"
EnableNodeSelection="True"
ExpandStateAtStartUp="AllNodesExpanded"
ReadOnly="True"
SupportNodeImages="True"
VisualStyle="Metro"
ItemsSource="SoccerMarkets"
>
<!-- Code for GridTreeControl Columns -->
<syncfusion:GridTreeControl.Columns>
<syncfusion:GridTreeColumn HeaderText="Nation" MappingName="{Binding RoughCat}"></syncfusion:GridTreeColumn>
</syncfusion:GridTreeControl.Columns>
</syncfusion:GridTreeControl>
This the snippet from MainWindow.xaml.cs where the DataContext is set:
public MainWindow()
{
DataContext = this;
InitializeComponent();
SkinStorage.SetVisualStyle(_tabControl, "Metro");
_settingsVM = new AppSettingsVM();
_txtBetdaqUser.DataContext = _settingsVM;
_chkSystemActive.DataContext = _settingsVM;
_chkInSimulationMode.DataContext = _settingsVM;
_mechanic = new TippMechanic(_settingsVM);
_soccerMarketsVM = new SoccerMarketVM();
Task[] tasks = new Task[1];
tasks[0] = Task.Factory.StartNew(async () => await _mechanic.Init());// _mechanic.Init();
Task.WaitAll(tasks);
_soccerMarketsVM.SoccerMarkets = _mechanic.SoccerMarketManager.SoccerMarkets;
_treeGrid.DataContext = _soccerMarketsVM.SoccerMarkets;
}
My ViewModel (_soccerMarketsVM) is defined this way:
class SoccerMarketVM : ObservableObject
{
private ObservableCollection<SoccerMarket> _soccerMarkets;
public ObservableCollection<SoccerMarket> SoccerMarkets
{
get { return _soccerMarkets; }
set
{
if(_soccerMarkets != null)
_soccerMarkets.CollectionChanged -= _soccerMarkets_CollectionChanged;
_soccerMarkets = value;
_soccerMarkets.CollectionChanged += _soccerMarkets_CollectionChanged;
}
}
public SoccerMarketVM()
{
//_soccerMarkets = new ObservableCollection<SoccerMarket>();
//_soccerMarkets.CollectionChanged += _soccerMarkets_CollectionChanged;
}
void _soccerMarkets_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine(e.Action.ToString());
}
}
The Events for CollectionChanged are fired and I get the Console.Writeline output.
Does anyone see's something wrong here?
In GridTreeControl, you can populate the data using different ways. In your code snippet, ItemsSource defined without specifying the Binding keyword and the MappingName is defined with Binding keyword. But for itemssource, you need to specify binding and for mapping name, you can directly assign property name without specifying binding. Please refer the below UG link of data population in GridTreeControl,
Link:
http://help.syncfusion.com/ug/wpf/index.html#!Documents/addingthegridtreecontroltoawpfapplication.htm
Elavarasan M – Syncfusion Software.

How to setup a Gtk# TreeModelFilter that filters an underlying TreeStore?

I have read the "GtkSharp TreeView tutorial" wherein the author describes how to setup and use a TreeModelFilter for an underlying ListStore ( under the tutorial section "Filtering Data"). The technique doesn't seem to work for an underlying hierarchical TreeStore. I want to filter a multilevel TreeStore and show the results in a TreeView. Its giving me a real hard time. Are there any tutorials, samples, or suggestions for doing it ?
Following is the code. Its basically the same code as the tutorial except for changes to deal with construction and population of a TreeStore rather than a ListStore.
{The TreeStore is used to save "names" and "email addresses" of contacts , divided into (and saved as) children of the roots "friends" and "relatives" }
// compilation requires references to:
// gtk-sharp, atk-sharp and glib-sharp
using System;
using Gtk;
public class TreeViewExample
{
public static void Main()
{
Gtk.Application.Init();
new TreeViewExample();
Gtk.Application.Run();
}
Gtk.Entry filterEntry;
Gtk.TreeModelFilter filter;
public TreeViewExample()
{
// Create a Window
Gtk.Window window = new Gtk.Window("TreeView Example");
window.SetSizeRequest(500, 200);
window.DeleteEvent += delegate { Application.Quit(); };
// Create an Entry used to filter the tree
filterEntry = new Gtk.Entry();
// Fire off an event when the text in the Entry changes
filterEntry.Changed += OnFilterEntryTextChanged;
// Create a nice label describing the Entry
Gtk.Label filterLabel = new Gtk.Label("Search:");
// Put them both into a little box so they show up side by side
Gtk.HBox filterBox = new Gtk.HBox();
filterBox.PackStart(filterLabel, false, false, 5);
filterBox.PackStart(filterEntry, true, true, 5);
// Create our TreeView
Gtk.TreeView tv = new Gtk.TreeView();
// Create a box to hold the Entry and Tree
Gtk.VBox box = new Gtk.VBox();
// Add the widgets to the box
box.PackStart(filterBox, false, false, 5);
box.PackStart(tv, true, true, 5);
window.Add(box);
//setting up columns and renderers
Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn { Title = "Name" };
Gtk.CellRendererText nameCell = new Gtk.CellRendererText();
nameColumn.PackStart(nameCell, true);
Gtk.TreeViewColumn emailColumn = new Gtk.TreeViewColumn { Title = "Email" };
Gtk.CellRendererText emailCell = new Gtk.CellRendererText();
emailColumn.PackStart(emailCell, true);
// Add the columns to the TreeView
tv.AppendColumn(nameColumn);
tv.AppendColumn(emailColumn);
// Tell the Cell Renderers which items in the model to display
nameColumn.AddAttribute(nameCell, "text", 0);
emailColumn.AddAttribute(emailCell, "text", 1);
// Create a model that will hold two strings
Gtk.TreeStore contacts = new Gtk.TreeStore(typeof(string), typeof(string));
// Add some hierarchical data
Gtk.TreeIter treeiter;
//first root
treeiter = contacts.AppendValues("FRIENDS");
// 2 children of first root
contacts.AppendValues(treeiter, "Ogre", "stinky#hotmale.com");
contacts.AppendValues(treeiter, "Bee", "stingy#coolguy.com");
// second root
treeiter = contacts.AppendValues("RELATIVES");
// 3 children of second root
contacts.AppendValues(treeiter, "Mommy", "mother#family.com");
contacts.AppendValues(treeiter, "Daddy", "father#family.com");
contacts.AppendValues(treeiter, "tom", "cousin#family.com");
filter = new Gtk.TreeModelFilter(contacts, null);
// Specify the function that determines which rows to filter out and which ones to display
filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc(FilterTree);
// Assign the filter as our treeview's model
tv.Model = filter;
// Show the window and everything on it
window.ShowAll();
}
private void OnFilterEntryTextChanged(object o, System.EventArgs args)
{
// Since the filter text changed, tell the filter to re-determine which rows to display
filter.Refilter();
}
private bool FilterTree(Gtk.TreeModel model, Gtk.TreeIter iter)
{
string contactname = model.GetValue(iter, 0).ToString();
if (filterEntry.Text == "")
return true;
if (contactname.IndexOf(filterEntry.Text) > -1)
return true;
else
return false;
}
}
[I am using mono 2.6.4 /monodevelop 2.4 / gtk-sharp 2.12 on windows vista.]
It seems that when filtering rows in a tree model, a row is only visible if ALL its parents are visible too. Since your filter function hides the parent nodes, it will not display the child nodes even if the text matches. I have modified your code to illustrate this problem:
Now, one of the parent nodes begins with 'test'. If you type 'test' you'll see the filtering works correctly.
using System;
using Gtk;
public class TreeViewExample
{
public static void Main ()
{
Gtk.Application.Init ();
new TreeViewExample ();
Gtk.Application.Run ();
}
Gtk.Entry filterEntry;
Gtk.TreeModelFilter filter;
public TreeViewExample ()
{
// Create a Window
Gtk.Window window = new Gtk.Window ("TreeView Example");
window.SetSizeRequest (500,200);
window.DeleteEvent+=delegate {Application.Quit();};
// Create an Entry used to filter the tree
filterEntry = new Gtk.Entry ();
// Fire off an event when the text in the Entry changes
filterEntry.Changed += OnFilterEntryTextChanged;
// Create a nice label describing the Entry
Gtk.Label filterLabel = new Gtk.Label ("Search:");
// Put them both into a little box so they show up side by side
Gtk.HBox filterBox = new Gtk.HBox ();
filterBox.PackStart (filterLabel, false, false, 5);
filterBox.PackStart (filterEntry, true, true, 5);
// Create our TreeView
Gtk.TreeView tv = new Gtk.TreeView ();
// Create a box to hold the Entry and Tree
Gtk.VBox box = new Gtk.VBox ();
// Add the widgets to the box
box.PackStart (filterBox, false, false, 5);
box.PackStart (tv, true, true, 5);
window.Add (box);
//setting up columns and renderers
Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn{Title="Name"};
Gtk.CellRendererText nameCell = new Gtk.CellRendererText ();
nameColumn.PackStart (nameCell, true);
Gtk.TreeViewColumn emailColumn = new Gtk.TreeViewColumn {Title="Email"};
Gtk.CellRendererText emailCell = new Gtk.CellRendererText ();
emailColumn.PackStart (emailCell, true);
// Add the columns to the TreeView
tv.AppendColumn (nameColumn);
tv.AppendColumn (emailColumn);
// Tell the Cell Renderers which items in the model to display
nameColumn.AddAttribute (nameCell, "text", 0);
emailColumn.AddAttribute (emailCell, "text", 1);
// Create a model that will hold two strings
Gtk.TreeStore contacts = new Gtk.TreeStore (typeof (string), typeof (string));
// Add some hierarchical data
Gtk.TreeIter treeiter;
//first root
treeiter= contacts.AppendValues("testFRIENDS");
// 2 children of first root
contacts.AppendValues(treeiter, "testOgre", "stinky#hotmale.com");
contacts.AppendValues(treeiter, "testBee", "stingy#coolguy.com");
// second root
treeiter= contacts.AppendValues("RELATIVES");
// 3 children of second root
contacts.AppendValues (treeiter,"Mommy","mother#family.com");
contacts.AppendValues (treeiter,"Daddy", "father#family.com");
contacts.AppendValues (treeiter,"tom", "cousin#family.com");
filter = new Gtk.TreeModelFilter (contacts, null);
// Specify the function that determines which rows to filter out and which ones to display
filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc (FilterTree);
// Assign the filter as our treeview's model
tv.Model = filter;
// Show the window and everything on it
window.ShowAll ();
}
private void OnFilterEntryTextChanged (object o, System.EventArgs args)
{
// Since the filter text changed, tell the filter to re-determine which rows to display
filter.Refilter ();
}
private bool FilterTree (Gtk.TreeModel model, Gtk.TreeIter iter)
{
string contactname = model.GetValue (iter, 0).ToString ();
if (filterEntry.Text == "")
return true;
if (contactname.IndexOf (filterEntry.Text) > -1)
return true;
else
return false;
}
}
The easiest solution with your current structure would be having the filter function always return TRUE for the 'container' nodes (Friends and Relatives), based upon a value in a hidden column in the model. It will not look exactly look the way you want, but it will work.
The GTK+ Treeview Tutorial, though not updated for some time,is still a VERY useful resource for all your TreeView needs. The code and examples are in C, but most of it still applies to GTK#.
In order to reach correct functionality of your code, I suggest you to modify it in following manner:
1.Add new field private filterBool = false; to your class
2.Modify your FilterTree method to this state:
private bool FilterTree (Gtk.TreeModel model, Gtk.TreeIter iter)
{
string contactname = model.GetValue (iter, 0).ToString ();
if (filterEntry.Text == "")
return true;
if (contactname.IndexOf (filterEntry.Text) > -1)
return true;
if (model.IterHasChild(iter))
{
filerBool = false;
investigateChildNodes(model, iter); //method checking if currently investigated
//node has any child fulfilling filter contitions
return filerBool;
}
return false;
}
3.Add missing method
private void investigateChildNodes(TreeModel model, TreeIter iter)
{
TreeIter childIter;
model.IterChildren(out childIter, iter);
do
{
if (model.GetValue(childIter, 0).ToString().IndexOf(filterEntry.Text) > -1)
filerBool = true;
if (model.IterHasChild(childIter))
investigateChildNodes(model, childIter);
} while (model.IterNext(ref childIter));
}
With this modification every node is checked for possible child nodes, which might fulfill filtering conditions. If any is detected, the node is not discarded.
Tinki version works flawlessly. The only thing which I do not like is the private variable. This can be eliminated by using a return value in the InvestigateChildrenNodes function.
private bool InvestigateChildNodes(TreeModel model, TreeIter iter)
{
TreeIter childIter;
model.IterChildren(out childIter, iter);
bool result = false;
do
{
if (model.GetValue(childIter, 0).ToString().Contains(FilterEntry.Text))
{
result = true;
break;
}
if (model.IterHasChild(childIter))
{
result = InvestigateChildNodes(model, childIter);
if (result)
break;
}
} while (model.IterNext(ref childIter));
return result;
}

Categories

Resources