-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ListView sample with code only and ListItem template
- Loading branch information
1 parent
cf24fd8
commit 52e500b
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
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; | ||
} | ||
} |