-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
365 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace XstarS.ComponentModel | ||
{ | ||
/// <summary> | ||
/// 提供位域枚举的向量视图的基类。 | ||
/// </summary> | ||
/// <typeparam name="TEnum">位域枚举的类型。</typeparam> | ||
[Serializable] | ||
public class EnumFlagsVectorView<TEnum> : EnumViewBase<TEnum> | ||
where TEnum : struct, Enum | ||
{ | ||
/// <summary> | ||
/// 初始化 <see cref="EnumFlagsVectorView{TEnum}"/> 类的新实例。 | ||
/// </summary> | ||
public EnumFlagsVectorView() { } | ||
|
||
/// <summary> | ||
/// 获取或设置当前视图表示的枚举值是否包含指定的枚举位域。 | ||
/// </summary> | ||
/// <param name="flagValue">要获取或设置的枚举位域。</param> | ||
/// <returns>若当前视图表示的枚举值包含指定的枚举位域, | ||
/// 则为 <see langword="true"/>;否则为 <see langword="false"/>。</returns> | ||
public bool this[TEnum flagValue] | ||
{ | ||
get => this.Value.HasFlag(flagValue); | ||
set => this.SetFlag(flagValue, value); | ||
} | ||
|
||
/// <summary> | ||
/// 获取当前视图表示的枚举值是否包含指定的枚举位域。 | ||
/// </summary> | ||
/// <param name="flagName">要确定的枚举位域的名称。</param> | ||
/// <returns>若当前视图表示的枚举值包含指定的枚举位域值, | ||
/// 则为 <see langword="true"/>;否则为 <see langword="false"/>。</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="flagName"/> 不为有效的枚举值名称。</exception> | ||
protected bool HasFlag( | ||
[CallerMemberName] string flagName = null) | ||
{ | ||
flagName = flagName ?? string.Empty; | ||
var flagValue = this.ParseEnum(flagName); | ||
return this[flagValue]; | ||
} | ||
|
||
/// <summary> | ||
/// 根据指示设置当前视图表示的枚举值的指定的枚举位域。 | ||
/// </summary> | ||
/// <param name="value">指示是添加位域还是移除位域。</param> | ||
/// <param name="flagName">要设置的枚举位域的名称。</param> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="flagName"/> 不为有效的枚举值名称。</exception> | ||
protected void SetFlag(bool value, | ||
[CallerMemberName] string flagName = null) | ||
{ | ||
flagName = flagName ?? string.Empty; | ||
var flagValue = this.ParseEnum(flagName); | ||
this[flagValue] = value; | ||
} | ||
|
||
/// <summary> | ||
/// 根据指示设置当前视图表示的枚举值的指定的枚举位域。 | ||
/// </summary> | ||
/// <param name="flagValue">要设置的枚举位域。</param> | ||
/// <param name="value">指示是否设置枚举值。</param> | ||
protected virtual void SetFlag(TEnum flagValue, bool value) | ||
{ | ||
var iValue = ((IConvertible)this.Value).ToUInt64(null); | ||
var iFlag = ((IConvertible)flagValue).ToUInt64(null); | ||
if (value) { iValue |= iFlag; } else { iValue &= ~iFlag; } | ||
this.Value = (TEnum)Enum.ToObject(typeof(TEnum), iValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
|
||
namespace XstarS.ComponentModel | ||
{ | ||
/// <summary> | ||
/// 表示枚举的列表视图。 | ||
/// </summary> | ||
/// <typeparam name="TEnum">枚举的类型。</typeparam> | ||
[Serializable] | ||
[DebuggerDisplay("Selected = {" + nameof(SelectedItem) + "}")] | ||
public class EnumListView<TEnum> : ReadOnlyObservableCollection<TEnum> | ||
where TEnum : struct, Enum | ||
{ | ||
/// <summary> | ||
/// 表示当前视图选中的枚举值的索引。 | ||
/// </summary> | ||
private int ItemIndex; | ||
|
||
/// <summary> | ||
/// 初始化 <see cref="EnumListView{TEnum}"/> 类的新实例。 | ||
/// </summary> | ||
public EnumListView() : base(EnumListView<TEnum>.CreateItems()) { } | ||
|
||
/// <summary> | ||
/// 创建当前枚举类型中所有枚举值的集合。 | ||
/// </summary> | ||
/// <returns>当前枚举类型中所有枚举值的集合。</returns> | ||
private static ObservableCollection<TEnum> CreateItems() | ||
{ | ||
var values = (TEnum[])Enum.GetValues(typeof(TEnum)); | ||
return new ObservableCollection<TEnum>(values); | ||
} | ||
|
||
/// <summary> | ||
/// 获取或设置当前视图选中的枚举值的索引。 | ||
/// </summary> | ||
/// <returns>当前视图表示的枚举值的索引。</returns> | ||
/// <exception cref="ArgumentOutOfRangeException"> | ||
/// <paramref name="value"/> 不为小于当前枚举值数量的非负整数。</exception> | ||
public int SelectedIndex | ||
{ | ||
get => this.ItemIndex; | ||
set => this.SelectIndex(value); | ||
} | ||
|
||
/// <summary> | ||
/// 获取或设置当前视图选中的枚举值。 | ||
/// </summary> | ||
/// <returns>当前视图选中的枚举值。</returns> | ||
/// <exception cref="ArgumentOutOfRangeException"> | ||
/// <paramref name="value"/> 不为有效的枚举值。</exception> | ||
public TEnum SelectedItem | ||
{ | ||
get => this[this.SelectedIndex]; | ||
set => this.SelectedIndex = this.IndexOf(value); | ||
} | ||
|
||
/// <summary> | ||
/// 设置当前视图选中的枚举值的索引。 | ||
/// </summary> | ||
/// <param name="index">要设置的枚举值的索引。</param> | ||
/// <exception cref="ArgumentOutOfRangeException"> | ||
/// <paramref name="index"/> 不为小于当前枚举值数量的非负整数。</exception> | ||
protected virtual void SelectIndex(int index) | ||
{ | ||
if ((index < 0) || (index >= this.Count)) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
if (this.ItemIndex != index) | ||
{ | ||
this.ItemIndex = index; | ||
this.NotifyPropertyChanged(nameof(this.SelectedIndex)); | ||
this.NotifyPropertyChanged(nameof(this.SelectedItem)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 通知指定属性的值已更改。 | ||
/// </summary> | ||
/// <param name="propertyName">已更改属性的名称。</param> | ||
protected void NotifyPropertyChanged(string propertyName) | ||
{ | ||
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace XstarS.ComponentModel | ||
{ | ||
/// <summary> | ||
/// 提供枚举的视图的抽象基类。 | ||
/// </summary> | ||
/// <typeparam name="TEnum">枚举的类型。</typeparam> | ||
[Serializable] | ||
[DebuggerDisplay("Value = {" + nameof(Value) + "}")] | ||
public abstract class EnumViewBase<TEnum> : ObservableDataObject | ||
where TEnum : struct, Enum | ||
{ | ||
/// <summary> | ||
/// 初始化 <see cref="EnumVectorView{TEnum}"/> 类的新实例。 | ||
/// </summary> | ||
protected EnumViewBase() { } | ||
|
||
/// <summary> | ||
/// 获取或设置当前视图表示的枚举值。 | ||
/// </summary> | ||
/// <returns>当前视图表示的枚举值。</returns> | ||
public TEnum Value | ||
{ | ||
get => this.GetProperty<TEnum>(); | ||
set => this.SetEnumValue(value); | ||
} | ||
|
||
/// <summary> | ||
/// 将当前枚举类型的枚举名称转换为等效的枚举值。 | ||
/// </summary> | ||
/// <param name="name">要转换的枚举值的名称。</param> | ||
/// <returns>名为 <paramref name="name"/> 的枚举值。</returns> | ||
protected TEnum ParseEnum(string name) | ||
{ | ||
return (TEnum)Enum.Parse(typeof(TEnum), name); | ||
} | ||
|
||
/// <summary> | ||
/// 设置当前视图表示的枚举值。 | ||
/// </summary> | ||
/// <param name="value">要设置的枚举值。</param> | ||
protected virtual void SetEnumValue(TEnum value) | ||
{ | ||
var enumValue = this.Value; | ||
this.SetProperty(value, nameof(this.Value)); | ||
var valueChanged = !object.Equals(enumValue, value); | ||
if (valueChanged) { this.NotifyEnumPropertiesChanged(); } | ||
} | ||
|
||
/// <summary> | ||
/// 通知所有枚举值对应的属性的值已更改。 | ||
/// </summary> | ||
protected void NotifyEnumPropertiesChanged() | ||
{ | ||
var enumNames = Enum.GetNames(typeof(TEnum)); | ||
Array.ForEach(enumNames, this.NotifyPropertyChanged); | ||
this.NotifyPropertyChanged(ObservableDataObject.IndexerName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.