Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.8 KiB

using MultiTerm.Protocols.Model;
using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
namespace MultiTerm.Wpf.CustomControl;
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:MultiTerm.Wpf.CustomControl.MultiFormatDataView"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:MultiTerm.Wpf.CustomControl.MultiFormatDataView;assembly=MultiTerm.Wpf.CustomControl.MultiFormatDataView"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Browse to and select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:MultiFormatDataView/>
///
/// </summary>
public class MultiFormatDataView : Control
{
#region Dependency Properties
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register("DataSource",
typeof(IEnumerable), typeof(MultiFormatDataView),
new PropertyMetadata(null, OnDataSourcePropertyChanged));
/// <summary>
/// .NET Property for DataSource.
/// </summary>
[Bindable(true)]
public IEnumerable DataSource
{
get { return (IEnumerable)GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
#endregion
static MultiFormatDataView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// extract instance and guard null
if (d is not MultiFormatDataView mfdv) { return; }
// extract instance of new Value and guard null
if (e.NewValue is not IEnumerable enumerable) { return; }
// check if enumerable items are all of correct type
foreach (var item in enumerable)
{
if (item is not DataLine)
{ throw new ArgumentException($"{nameof(DataSourceProperty)} must be of type {nameof(DataLine)}"); }
}
}
}