Added event when a text element gets selected
This commit is contained in:
parent
0e4a99332b
commit
af47eb606c
@ -7,12 +7,21 @@ namespace Processor;
|
||||
|
||||
public static class TextElementProcessor
|
||||
{
|
||||
// Load all text elements for one particular section:
|
||||
public static async Task<List<TextElement>> GetTextElements(Section section)
|
||||
{
|
||||
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
||||
return await db.TextElements.Where(n => n.Section == section).OrderBy(n => n.Name).ThenBy(n => n.Id).ToListAsync();
|
||||
}
|
||||
|
||||
// Load one text element by id:
|
||||
public static async Task<TextElement> LoadTextElement(int id)
|
||||
{
|
||||
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
||||
return await db.TextElements.FirstAsync(n => n.Id == id);
|
||||
}
|
||||
|
||||
// Adds a new text element:
|
||||
public static async Task<ProcessorResult<TextElement>> AddTextElement(string? currentSectionKey, string elementName)
|
||||
{
|
||||
await using var db = ProcessorMeta.ServiceProvider.GetRequiredService<DataContext>();
|
||||
|
@ -4,7 +4,15 @@ namespace UI_WinForms;
|
||||
|
||||
internal static class AppEvents
|
||||
{
|
||||
// Section changed event which can be subscribed:
|
||||
internal static event EventHandler<Section> WhenSectionChanged;
|
||||
|
||||
// Method to raise the section changed event:
|
||||
internal static void SectionChanged(Section section) => AppEvents.WhenSectionChanged?.Invoke(null, section);
|
||||
|
||||
// Text element changed event which can be subscribed:
|
||||
internal static event EventHandler<TextElement> WhenTextElementChanged;
|
||||
|
||||
// Method to raise the text element changed event:
|
||||
internal static void TextElementChanged(TextElement textElement) => AppEvents.WhenTextElementChanged?.Invoke(null, textElement);
|
||||
}
|
@ -169,6 +169,7 @@
|
||||
this.listTextElements.TabIndex = 5;
|
||||
this.listTextElements.UseCompatibleStateImageBehavior = false;
|
||||
this.listTextElements.View = System.Windows.Forms.View.Details;
|
||||
this.listTextElements.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listTextElements_ItemSelectionChanged);
|
||||
//
|
||||
// column
|
||||
//
|
||||
|
@ -8,6 +8,7 @@ namespace UI_WinForms.Components;
|
||||
public partial class TextElements : UserControl
|
||||
{
|
||||
private Section? currentSection;
|
||||
private TextElement? currentTextElement;
|
||||
|
||||
public TextElements()
|
||||
{
|
||||
@ -30,15 +31,24 @@ public partial class TextElements : UserControl
|
||||
AppEvents.WhenSectionChanged += async (sender, section) =>
|
||||
{
|
||||
this.currentSection = section;
|
||||
this.currentTextElement = null;
|
||||
this.buttonAdd.Enabled = this.currentSection is not null;
|
||||
this.buttonRename.Enabled = this.buttonRemove.Enabled = false;
|
||||
|
||||
// Update the path:
|
||||
if (this.currentSection is null)
|
||||
return;
|
||||
|
||||
// Update the path:
|
||||
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
|
||||
this.LoadTextElements();
|
||||
};
|
||||
|
||||
// When the text element is changed, update the button states:
|
||||
AppEvents.WhenTextElementChanged += (sender, textElement) =>
|
||||
{
|
||||
this.currentTextElement = textElement;
|
||||
this.buttonRename.Enabled = this.buttonRemove.Enabled = this.currentTextElement is not null;
|
||||
};
|
||||
}
|
||||
|
||||
// Loads all the text elements for the current section.
|
||||
@ -55,7 +65,7 @@ public partial class TextElements : UserControl
|
||||
foreach (var textElement in textElements)
|
||||
this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0)
|
||||
{
|
||||
Tag = textElement.Code,
|
||||
Tag = textElement.Id,
|
||||
});
|
||||
|
||||
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
||||
@ -86,7 +96,7 @@ public partial class TextElements : UserControl
|
||||
// Add the text element to the list:
|
||||
this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0)
|
||||
{
|
||||
Tag = newTextElement.Result.Code,
|
||||
Tag = newTextElement.Result.Id,
|
||||
});
|
||||
|
||||
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
||||
@ -101,4 +111,14 @@ public partial class TextElements : UserControl
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
|
||||
{
|
||||
// Load the text element:
|
||||
var selectedTextElementId = (int)e.Item.Tag;
|
||||
this.currentTextElement = await TextElementProcessor.LoadTextElement(selectedTextElementId);
|
||||
|
||||
// Fire the event:
|
||||
AppEvents.TextElementChanged(this.currentTextElement);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user