-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace ListViewSample; | ||
|
||
using Gtk; | ||
using static Gtk.SignalListItemFactory; | ||
|
||
public class CodeListViewWindow : Window | ||
{ | ||
public CodeListViewWindow() | ||
: base() | ||
{ | ||
Title = "Code ListView"; | ||
SetDefaultSize(300, 300); | ||
|
||
var stringList = StringList.New(["One", "Two", "Three", "Four"]); | ||
Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs
|
||
var selectionModel = NoSelection.New(stringList); | ||
var listItemFactory = SignalListItemFactory.New(); | ||
listItemFactory.OnSetup += SetupSignalHandler; | ||
listItemFactory.OnBind += BindSignalHandler; | ||
|
||
var listView = ListView.New(selectionModel, listItemFactory); | ||
|
||
var scrolledWindow = ScrolledWindow.New(); | ||
scrolledWindow.Child = listView; | ||
Child = scrolledWindow; | ||
} | ||
|
||
private void SetupSignalHandler(SignalListItemFactory sender, SetupSignalArgs args) | ||
{ | ||
var listItem = args.Object as ListItem; | ||
if (listItem is null) | ||
{ | ||
return; | ||
} | ||
|
||
var label = Label.New(null); | ||
listItem.Child = label; | ||
} | ||
|
||
private void BindSignalHandler(SignalListItemFactory sender, BindSignalArgs args) | ||
{ | ||
var listItem = args.Object as ListItem; | ||
if (listItem is null) | ||
{ | ||
return; | ||
} | ||
|
||
var label = listItem.Child as Label; | ||
if (label is null) | ||
{ | ||
return; | ||
} | ||
|
||
var item = listItem.Item as StringObject; | ||
label.SetText(item?.String ?? "NOT FOUND"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<interface> | ||
<template class="GtkListItem"> | ||
<property name="child"> | ||
<object class="GtkLabel"> | ||
<binding name="label"> | ||
<lookup name="string" type="GtkStringObject"> | ||
<lookup name="item">GtkListItem</lookup> | ||
</lookup> | ||
</binding> | ||
</object> | ||
</property> | ||
</template> | ||
</interface> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="**\*.ui" /> | ||
<EmbeddedResource Include="**/*.ui"> | ||
<LogicalName>%(Filename)%(Extension)</LogicalName> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Libs\Gtk-4.0\Gtk-4.0.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var application = Gtk.Application.New("org.kashif-code-samples.listview.sample", Gio.ApplicationFlags.FlagsNone); | ||
application.OnActivate += (sender, args) => | ||
{ | ||
var buttonShowCodeListView = CreateButton("Show Code ListView Window"); | ||
buttonShowCodeListView.OnClicked += (_, _) => new ListViewSample.CodeListViewWindow().Show(); | ||
|
||
var buttonShowTemplateListView = CreateButton("Show Template ListView Window"); | ||
buttonShowTemplateListView.OnClicked += (_, _) => new ListViewSample.TemplateListViewWindow().Show(); | ||
|
||
var gtkBox = Gtk.Box.New(Gtk.Orientation.Vertical, 0); | ||
gtkBox.Append(buttonShowCodeListView); | ||
gtkBox.Append(buttonShowTemplateListView); | ||
|
||
var window = Gtk.ApplicationWindow.New((Gtk.Application) sender); | ||
window.Title = "ListView Sample"; | ||
window.SetDefaultSize(300, 300); | ||
window.Child = gtkBox; | ||
window.Show(); | ||
}; | ||
return application.RunWithSynchronizationContext(null); | ||
|
||
static Gtk.Button CreateButton(string label) | ||
{ | ||
var button = Gtk.Button.New(); | ||
button.Label = label; | ||
button.SetMarginTop(12); | ||
button.SetMarginBottom(12); | ||
button.SetMarginStart(12); | ||
button.SetMarginEnd(12); | ||
return button; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace ListViewSample; | ||
|
||
using System.Reflection; | ||
using Gtk; | ||
using GObject; | ||
using GLib; | ||
|
||
public class TemplateListViewWindow : Window | ||
{ | ||
public TemplateListViewWindow() | ||
: base() | ||
{ | ||
Title = "Template ListView"; | ||
SetDefaultSize(300, 300); | ||
|
||
var stringList = StringList.New(["One", "Two", "Three", "Four"]); | ||
Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs
|
||
var selectionModel = SingleSelection.New(stringList); | ||
var bytes = Assembly.GetExecutingAssembly() | ||
.ReadResourceAsByteArray("ListItemTemplate.ui"); | ||
var listItemFactory = BuilderListItemFactory.NewFromBytes(null, Bytes.New(bytes)); | ||
var listView = ListView.New(selectionModel, listItemFactory); | ||
|
||
var scrolledWindow = ScrolledWindow.New(); | ||
scrolledWindow.Child = listView; | ||
Child = scrolledWindow; | ||
} | ||
} |