Posts: 36
Threads: 21
Joined: Mar 2022
It's possible to create something like this with wpf, i'm still new to C#.
Posts: 12,095
Threads: 142
Joined: Dec 2002
09-26-2022, 05:47 AM
(This post was last modified: 09-26-2022, 08:38 AM by Gintaras.)
// script "Dialog with ListView.cs"
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
var d = new DialogListViewExample();
if (d.ShowDialog() != true) return;
class DialogListViewExample : Window {
ListView _lv; //this control will display data
List<Abc> _a; //data that will be displayed
ComboBox _cb1, _cb2; //other controls
//listview item data type example
record Abc(string Name, string Value) { }
public DialogListViewExample() {
//create dialog
Title = "ListView example";
var b = new wpfBuilder(this).WinSize(600, 500).Columns(-1, -1, 70);
b.R.Add(out _cb1).Items("|ffff|gggg");
b.Add(out _cb2).Items("|vvvv|nnnn");
b.AddButton("Update", _ => _Update());
b.Row(-1).Add(out _lv);
b.End();
//add listview columns and bind to data
var gv = new GridView { AllowsColumnReorder = false };
gv.Columns.Add(new() { Header = "A", Width = 150, DisplayMemberBinding = new Binding("Name") { Mode = BindingMode.OneTime } });
gv.Columns.Add(new() { Header = "B", Width = 300, DisplayMemberBinding = new Binding("Value") { Mode = BindingMode.OneTime } });
_lv.View = gv;
//data
_a = new() {
new("One", "jhsjhjs"),
new("Two", "mcnvmncm"),
};
_lv.ItemsSource = _a;
//item selected event
_lv.SelectionChanged += (_, e) => {
if (_lv.SelectedItem is Abc x) {
print.it("selected", x);
}
};
//item double-clicked event
_lv.PreviewMouseDoubleClick += (o, e) => {
e.Handled = true;
if (e.ChangedButton == MouseButton.Left && _EventItemData(e) is Abc x) {
print.it("double-clicked", x);
}
};
//context menu event
_lv.ContextMenuOpening += (o, e) => {
e.Handled = true;
if (_EventItemData(e) is Abc x) {
print.it("context menu", x);
} else {
print.it("context menu");
}
};
//b.Loaded += () => {
//};
#if WPF_PREVIEW //menu Edit -> View -> WPF preview
b.Window.Preview();
#endif
//b.OkApply += e => {
//};
}
//example of updating data
void _Update() {
_a.RemoveAt(0);
_a.Add(new("Three", "qwwqewe"));
_lv.ItemsSource = null;
_lv.ItemsSource = _a;
}
static DependencyObject _EventItem(RoutedEventArgs e) => ((ItemsControl)e.Source).ContainerFromElement(e.OriginalSource as DependencyObject);
static object _EventItemData(RoutedEventArgs e) => _EventItem(e) is ContentControl c ? c.Content : null;
}
Posts: 12,095
Threads: 142
Joined: Dec 2002
This is editable.
// script "Dialog - DataGrid.cs"
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Windows.Data;
//create dialog
var b = new wpfBuilder("DataGrid example").WinSize(400, 400);
b.Row(-1).Add(out DataGrid g);
b.R.AddOkCancel();
b.End();
//columns, properties
g.Columns.Add(new DataGridCheckBoxColumn { Binding = new Binding("Check"), Width = 20 });
g.Columns.Add(new DataGridTextColumn { Header = "Editable", Binding = new Binding("Text"), Width = new(1, DataGridLengthUnitType.Star) });
g.Columns.Add(new DataGridTextColumn { Header = "Readonly", Binding = new Binding("Comment"), IsReadOnly = true, Width = 100 });
g.AutoGenerateColumns = false;
g.AlternatingRowBackground = Brushes.Wheat;
g.GridLinesVisibility = DataGridGridLinesVisibility.Vertical;
g.VerticalGridLinesBrush = Brushes.LightGray;
//g.IsReadOnly = true;
//data
var a = new ObservableCollection<Abc>() {
new(true, "text 1"),
new(!true, "text 2", "comment"),
new(true, "text 3"),
new(!true, "text 4"),
};
g.ItemsSource = a;
//single-click editing
g.PreviewMouseDown += (o, e) => {
if (e.ChangedButton == MouseButton.Left) {
g.Dispatcher.InvokeAsync(() => g.BeginEdit(e), System.Windows.Threading.DispatcherPriority.Input);
}
};
if (!b.ShowDialog()) return;
print.it(a); //automatically updated when the user edits cells
//row data type
record Abc(bool Check, string Text, string Comment = null) { }
Posts: 36
Threads: 21
Joined: Mar 2022
Thanks, both worked great.