1. Summary
This article will explain the functions of implementing a message center based on WPF. For example, common software will often receive messages such as "news" and "announcements" pushed by the server. At this time, this requirement needs to be analyzed.
Functional analysis is as follows:
- The message content is displayed.
- Message management add, delete, and batch delete.
- Message classification (notification type messages, interaction type messages, for example, can jump to a certain connection or module within a program)
- Message processing (accept, delete, ignore)
2. Realization

- Message content display The custom control considered here is a Listbox, and the message itself is the content of multiple items and requires manipulation of each item.
<ListBox
Grid.Row="1"
MaxHeight="335"
Background="{x:Null}"
BorderThickness="0"
ItemContainerStyle="{DynamicResource ListBoxItemStyle}"
ItemsSource="{Binding MessageItem}"
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type localModel:MessageItemModel}">
<Border
Height="30"
BorderBrush="#FFBDBDBD"
BorderThickness="0,0,0,0.6">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<DockPanel>
<Label
MaxWidth="70"
Content="{Binding Path=Name}"
Foreground="Red"
ToolTip="{Binding Path=Name}" />
<Label
MaxWidth="130"
Content="{Binding Path=Content}"
Foreground="White"
ToolTip="{Binding Path=Content}" />
</DockPanel>
<CheckBox
Grid.Column="1"
FlowDirection="RightToLeft"
IsChecked="{Binding Path=CheckBoxState}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
- Message management add, delete, and batch delete
After the main container is determined, each next message can be a custom ListboxItem, and specific processing must be carried out for each message.
For example, for a notification type message, only the OK button is needed.
Interactive type messages need to be confirmed, deleted, and ignored
<DataTemplate x:Key="SelectedTemplate" DataType="{x:Type localModel:MessageItemModel}">
<Border BorderBrush="#FFBDBDBD" BorderThickness="0,0,0,0.6">
<StackPanel>
<TextBox
MaxWidth="240"
MaxHeight="200"
Padding="0,5,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
FontSize="14"
Foreground="White"
IsReadOnly="True"
Text="{Binding Path=Content}"
TextAlignment="Center"
TextWrapping="WrapWithOverflow"
ToolTip="{Binding Path=Content}"
VerticalScrollBarVisibility="Auto" />
<StackPanel
Margin="5,5,5,9"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<Button
Width="60"
Height="25"
Margin="5"
Command="{Binding DataContext.ClickAcceptCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}"
Content="接受"
Style="{StaticResource BlueButtonStyle}"
Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
<Button
Width="60"
Height="25"
Margin="5"
Command="{Binding DataContext.ClickRefuseCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}"
Content="忽略"
Style="{StaticResource BlueButtonStyle}"
Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
<Button
Width="60"
Height="25"
Margin="5"
Command="{Binding DataContext.ClickAgreeCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}"
Content="确认"
Style="{StaticResource BlueButtonStyle}"
Visibility="{Binding Path=NoticeType, Converter={StaticResource BooleanToVisibilityConverter}}" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
- message classification
Here, you just need to define specific enumerations at the Model layer.
/// <summary>
/// 消息处理结果
/// </summary>
public enum SysMessageResult
{
/// <summary>
/// 未处理
/// </summary>
Unhandled = 0,
/// <summary>
/// 同意
/// </summary>
Processed = 1
}
/// <summary>
/// 消息类型
/// </summary>
public enum SysMessageType
{
/// <summary>
/// 通知类型
/// </summary>
NoticeType = 0,
/// <summary>
/// 其他类型
/// </summary>
OtherType = 1
}
- message processing
Message processing refers to the logic used by the three buttons of "OK","Accept" and "Ignore" to process message content. If your partner needs it, you can modify it according to your own needs. I define it here as follows:
- Confirm: Notification messages are usually processed, and processing simply removes the item from the message list and does no other action.
- Accept: is a button that handles the interaction type that handles removing the item from the message list and triggers other business processing behaviors.
- Ignore: Processes all types of messages, except that they are not displayed in the UI but will still exist in the message list to process the message next time or during idle time.