{"id":7472,"date":"2023-05-12T09:53:08","date_gmt":"2023-05-12T08:53:08","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=7472"},"modified":"2023-05-12T09:53:09","modified_gmt":"2023-05-12T08:53:09","slug":"xaml-selectdialog","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/xaml-selectdialog\/","title":{"rendered":"XAML SelectDialog"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Windows;\nusing System.Windows.Data;\n\nnamespace Plugin.Views.Controls\n{\n    public interface ISelectDialogOption\n    { \n        object Key { get; }\n        string Label { get; set; }\n        object Value { get; }\n    }\n\n    public class SelectDialogOption&lt;TKey, TValue> : ISelectDialogOption\n    {\n        public SelectDialogOption(TKey key, string label, TValue value)\n        {\n            Key = key;\n            Label = label;\n            Value = value;\n        }\n\n        public TKey Key { get; set; }\n        public string Label { get; set; }\n        public TValue Value { get; set; }\n        object ISelectDialogOption.Key => Key;\n        object ISelectDialogOption.Value => Label;\n    }\n    public partial class SelectDialog : Window\n    {\n        public SelectDialog(string question, string title)\n        {\n            InitializeComponent();\n            Loaded += new RoutedEventHandler(SelectDialog_Loaded);\n            txtQuestion.Text = question;\n            Title = title;\n        }\n\n        private CollectionView ListView => (CollectionView)CollectionViewSource.GetDefaultView(lvDetails?.ItemsSource);\n\n        public void InitOptions&lt;TKey, TValue>(IEnumerable&lt;SelectDialogOption&lt;TKey, TValue>> options, TKey defaultValue = default(TKey)) where TKey : class\n        {\n            lvDetails.ItemsSource = options;\n            ListView.Filter = CustomFilter;\n            var selectedItem = options.FirstOrDefault(x => x.Key == defaultValue);\n            if (selectedItem?.Key != null)\n                lvDetails.SelectedItem = selectedItem;\n        }\n\n        private bool CustomFilter(object item)\n        {\n            if (string.IsNullOrEmpty(txtFilter.Text)) return true;\n            if (item is ISelectDialogOption option)\n                return Search(GetSearchWords(txtFilter.Text), option.Label);\n            return false;\n        }\n\n        public static bool Search(List&lt;string> words, string label) => words.All(x => label.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0);\n        public static List&lt;string> GetSearchWords(string text) => text.Trim().Split(' ').Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();\n\n        public static TValue ShowSelectDialog&lt;TKey, TValue>(string question, string title, IEnumerable&lt;SelectDialogOption&lt;TKey, TValue>> options, TKey defaultValue = default(TKey)) where TKey : class\n        {\n            var inst = new SelectDialog(question, title);\n            inst.InitOptions(options, defaultValue);\n            inst.ShowDialog();\n            if (inst.DialogResult == true &amp;&amp; inst.SelectedItem is SelectDialogOption&lt;TKey, TValue> kv)\n                return kv.Value;\n            return default;\n        }\n\n        private void BtnOk_Click(object sender, RoutedEventArgs e)\n        {\n            if (SelectedItem == null) return;\n            DialogResult = true;\n            Close();\n        }\n\n        public object SelectedItem => lvDetails.SelectedItem;\n        void SelectDialog_Loaded(object sender, RoutedEventArgs e) => txtFilter.Focus();\n        private void BtnCancel_Click(object sender, RoutedEventArgs e) => Close();\n        private void txtFilter_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) => ListView.Refresh();\n    }\n}\n<\/pre><\/div>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"xml\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">&lt;Window xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n    x:Class=\"Plugin.Views.Controls.SelectDialog\"\n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n    WindowStartupLocation=\"CenterScreen\" \n    SizeToContent=\"WidthAndHeight\"\n    MinWidth=\"400\"\n    MinHeight=\"100\"\n    WindowStyle=\"SingleBorderWindow\"\n    ResizeMode=\"CanMinimize\">\n    &lt;StackPanel Margin=\"5\">\n        &lt;TextBlock Name=\"txtQuestion\" Margin=\"5\"\/>\n        &lt;TextBox DockPanel.Dock=\"Top\" Margin=\"0,0,0,10\" Name=\"txtFilter\" TextChanged=\"txtFilter_TextChanged\"  \/>\n        &lt;ListView Name=\"lvDetails\" Height=\"200\">\n            &lt;ListView.View>\n                &lt;GridView>\n                    &lt;GridViewColumn Header=\" \" Width=\"400\" DisplayMemberBinding=\"{Binding Label}\" \/>\n                &lt;\/GridView>\n            &lt;\/ListView.View>\n        &lt;\/ListView>\n        &lt;StackPanel Orientation=\"Horizontal\" Margin=\"5\" HorizontalAlignment=\"Right\">\n            &lt;Button Content=\"_OK\" IsDefault=\"True\" Margin=\"5\" Name=\"BtnOk\" Click=\"BtnOk_Click\" \/>\n            &lt;Button Content=\"_Cancel\" IsCancel=\"True\" Margin=\"5\" Name=\"BtnCancel\" Click=\"BtnCancel_Click\" \/>\n        &lt;\/StackPanel>\n    &lt;\/StackPanel>\n&lt;\/Window>\n<\/pre><\/div>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">var options = lookup.InventoryItems.Select(x => new SelectDialogOption&lt;string, CommonInvItemClient>(x.Item, $\"{x.GTIN} {x.Name} ({x.Item})\", x)).OrderBy(x => x.Label);\nvar invItem = SelectDialog.ShowSelectDialog(\"Zoek artikel (of GTIN) en selecteer om aan de orderregel te koppelen\", \"Artikel matchen\", options);\nif (invItem == null) return;\nvar invItemGTIN = invItem.GTIN ?? invItem.EAN;<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Example:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7472","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=7472"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7472\/revisions"}],"predecessor-version":[{"id":7755,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7472\/revisions\/7755"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=7472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=7472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=7472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}