Files
CustomerPanel-Test/UserControls/AnnouncementListItem.xaml.cs
2026-03-05 09:56:57 +01:00

283 lines
9.1 KiB
C#

using C4IT.API.Contracts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace C4IT_CustomerPanel.UserControls
{
/// <summary>
/// Interaktionslogik für AnnouncementListItem.xaml
/// </summary>
public partial class AnnouncementListItem : UserControl, INotifyPropertyChanged
{
public Announcement _announcementItem;
public Announcement AnnouncementItem
{
get => _announcementItem;
set
{
_announcementItem = value;
// PropertyChanged für die Haupt-Property
OnPropertyChanged(nameof(AnnouncementItem));
// und für alle gebundenen Felder
OnPropertyChanged(nameof(Subject));
OnPropertyChanged(nameof(Text));
OnPropertyChanged(nameof(Priority));
OnPropertyChanged(nameof(PrioColor));
OnPropertyChanged(nameof(IsUnread));
OnPropertyChanged(nameof(CreatedDate));
OnPropertyChanged(nameof(VisibleFrom));
}
}
public bool _PrepareForDelete;
private Boolean _isUnread;
public Boolean IsUnread
{
get { return _isUnread; }
set
{
_isUnread = value;
OnPropertyChanged();
}
}
public SolidColorBrush PrioColor
{
get { return _announcementItem.getPrioColorBrush(); }
set
{
OnPropertyChanged();
}
}
public Int32 Priority
{
get { return _announcementItem._priority; }
set
{
OnPropertyChanged();
}
}
public bool IsAdhoc
{
get { return _announcementItem._isAdhoc; }
set
{
_announcementItem._isAdhoc = value;
OnPropertyChanged();
}
}
public bool toRemove = false;
public DateTime CreatedDate
{
get { return _announcementItem._createdDate.ToLocalTime(); }
set
{
_announcementItem._createdDate = value;
OnPropertyChanged();
}
}
public DateTime? VisibleFrom
{
get { return _announcementItem._visibleFrom?.ToLocalTime(); }
set
{
_announcementItem._visibleFrom = value;
if (value.HasValue)
{
CreatedDate = value.Value;
}
OnPropertyChanged();
}
}
public string Subject
{
get { return _announcementItem._subject; }
set
{
_announcementItem._subject = value;
OnPropertyChanged();
}
}
public string Text
{
get { return _announcementItem._message; }
set
{
_announcementItem._message = value;
OnPropertyChanged();
}
}
public Guid getEOID()
{
return _announcementItem._objectID;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
int count = _announcementItem._message.Length;
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
if (string.IsNullOrWhiteSpace(_announcementItem._message))
{
this.Height = 70;
}
if (count <= 54 && !string.IsNullOrWhiteSpace(_announcementItem._message))
{
this.Height = 90;
}
}
#endregion
public AnnouncementListItem()
{
InitializeComponent();
GetParameterForAnnouncmentView();
this.DataContext = this;
}
private void OnAnnMouseEnter(object sender, MouseEventArgs e)
{
HighlightAnnouncementBorder.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2d45fb"));
}
private void OnAnnMouseLeave(object sender, MouseEventArgs e)
{
HighlightAnnouncementBorder.BorderBrush = new SolidColorBrush(Colors.White);
}
private void OnMouseLeftButtonDownClicked(object sender, MouseButtonEventArgs e)
{
MainWindow.MainInstance.AnnouncementCtrl.OpenAnnClicked(sender, e);
}
public void GetParameterForAnnouncmentView()
{
// Header + Announcement-Content (Standard-Auslieferungszustand)
if (MainWindow.MainInstance.ConfigSettings.AnnouncementView == 0)
{
txtContent.Visibility = Visibility.Visible;
txtNotice.Visibility = Visibility.Collapsed;
this.MinHeight = 0;
this.MaxHeight = 120;
}
// Only Header
if (MainWindow.MainInstance.ConfigSettings.AnnouncementView == 1)
{
txtContent.Visibility = Visibility.Collapsed;
txtNotice.Visibility = Visibility.Collapsed;
Thickness margin = txtSubject.Margin;
margin.Bottom = 20;
txtSubject.Margin = margin;
this.MinHeight = 70;
this.MaxHeight = 70;
}
// Header + Custom Text
if (!string.IsNullOrEmpty(MainWindow.MainInstance.ConfigSettings.CustomAnnouncementText))
{
txtContent.Visibility = Visibility.Collapsed;
txtNotice.Visibility = Visibility.Visible;
this.MaxHeight = 98;
this.MinHeight = 98;
txtNotice.Text = MainWindow.MainInstance.ConfigSettings.CustomAnnouncementText;
txtNotice.Loaded -= TxtNotice_Loaded;
txtNotice.Loaded += TxtNotice_Loaded;
txtNotice.SizeChanged -= TxtNotice_SizeChanged;
txtNotice.SizeChanged += TxtNotice_SizeChanged;
}
}
private void TxtNotice_Loaded(object sender, RoutedEventArgs e) => UpdateNoticeToolTip();
private void TxtNotice_SizeChanged(object sender, SizeChangedEventArgs e) => UpdateNoticeToolTip();
private void UpdateNoticeToolTip()
{
txtNotice.Dispatcher.BeginInvoke(new Action(() =>
{
ToolTipService.SetInitialShowDelay(txtNotice, 400);
bool moreThanTwoLines = ExceedsLineCount(txtNotice, 2);
ToolTipService.SetToolTip(txtNotice, moreThanTwoLines ? Properties.Resources.ShowMoreDetailsToolTip : null);
}), DispatcherPriority.Render);
}
private static bool ExceedsLineCount(TextBlock tb, int maxLines)
{
if (tb == null || string.IsNullOrEmpty(tb.Text))
return false;
if (tb.ActualWidth <= 0)
return false;
// Measure text as it is rendered (with actual width)
var measureTb = new TextBlock
{
Text = tb.Text,
FontFamily = tb.FontFamily,
FontSize = tb.FontSize,
FontStyle = tb.FontStyle,
FontWeight = tb.FontWeight,
FontStretch = tb.FontStretch,
FlowDirection = tb.FlowDirection,
TextWrapping = tb.TextWrapping,
LineHeight = tb.LineHeight,
LineStackingStrategy = tb.LineStackingStrategy,
TextTrimming = TextTrimming.None
};
// Calculate height for maxLines lines
double lineHeight = tb.LineHeight;
if (double.IsNaN(lineHeight) || lineHeight <= 0)
lineHeight = tb.FontSize * 1.2; // Fallback, in case LineHeight is not set
double maxHeight = maxLines * lineHeight;
measureTb.Measure(new Size(tb.ActualWidth, double.PositiveInfinity));
return measureTb.DesiredSize.Height > maxHeight + 0.5;
}
}
}