/// <summary>
/// Collection ViewModel Interface.
/// </summary>
/// <typeparam name="T">
/// </typeparam>
public interface ICollectionViewModel<T>
{
/// <summary>
/// 选中项发生改变.
/// </summary>
event EventHandler SelectedChanged;
/// <summary>
/// 双击项
/// </summary>
event EventHandler DoubleClick;
/// <summary>
/// 集合改变.
/// </summary>
event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// 双击命令.
/// </summary>
ICommand DoubleClickCommand { get; }
/// <summary>
/// 数据源.
/// </summary>
ReadOnlyObservableCollection<T> Collection { get; }
/// <summary>
/// 选中项.
/// </summary>
T SelectedItem { get; set; }
/// <summary>
/// 选中项索引.
/// </summary>
int SelectedIndex { get; set; }
/// <summary>
/// 选中项是否为空.
/// </summary>
bool SelectedIsNull { get; }
/// <summary>
/// 根据条件获取索引.
/// </summary>
/// <param name="predicate">
/// The predicate.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
int GetIndex(Func<T, bool> predicate);
/// <summary>
/// 根据条件获取项.
/// </summary>
/// <param name="predicate">
/// The predicate.
/// </param>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
T GetItem(Func<T, bool> predicate);
/// <summary>
/// 添加项.
/// </summary>
/// <param name="item">
/// The item.
/// </param>
void Add(T item);
/// <summary>
/// 清空集合.
/// </summary>
void Clear();
/// <summary>
/// 移除项.
/// </summary>
/// <param name="item">
/// The item.
/// </param>
void Remove(T item);
/// <summary>
/// 移除制定索引项.
/// </summary>
/// <param name="index">
/// The index.
/// </param>
void RemoveAt(int index);
/// <summary>
/// 转换为List.
/// </summary>
/// <returns>
/// The <see>
/// <cref>IList</cref>
/// </see>
/// .
/// </returns>
IList<T> ToList();
}
/// <summary>
/// The Collection ViewModel实现.
/// </summary>
/// <typeparam name="T">
/// <tag>泛型</tag>
/// </typeparam>
public class CollectionViewModel<T> : ViewModelBase, ICollectionViewModel<T>
{
/// <summary>
/// The seletected item.
/// </summary>
private T seletectedItem;
/// <summary>
/// The selected index.
/// </summary>
private int selectedIndex;
/// <summary>
/// Initializes a new instance of the <see cref="CollectionViewModel{T}"/> class.
/// </summary>
public CollectionViewModel()
{
this.DoubleClickCommand = new RelayCommand(
() =>
{
if (DoubleClick != null)
{
DoubleClick(this, new EventArgs());
}
});
this.ListCollection = new ObservableCollection<T>();
this.Collection = new ReadOnlyObservableCollection<T>(this.ListCollection);
this.ListCollection.CollectionChanged += (o, e) => this.OnCollectionChanged(e);
}
/// <summary>
/// The selected changed.
/// </summary>
public event EventHandler SelectedChanged;
/// <summary>
/// The double click.
/// </summary>
public event EventHandler DoubleClick;
/// <summary>
/// The collection changed.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Gets the double click command.
/// </summary>
public ICommand DoubleClickCommand { get; private set; }
/// <summary>
/// Gets or sets the page view model.
/// </summary>
public IListPageViewModel PageViewModel { get; set; }
/// <summary>
/// Gets a value indicating whether selected is null.
/// </summary>
public bool SelectedIsNull
{
get
{
return null == this.SelectedItem;
}
}
/// <summary>
/// Gets or sets the selected index.
/// </summary>
public int SelectedIndex
{
get
{
return this.selectedIndex;
}
set
{
this.selectedIndex = value;
this.RaisePropertyChanged("SelectedIndex");
}
}
/// <summary>
/// Gets or sets the selected item.
/// </summary>
public T SelectedItem
{
get
{
return this.seletectedItem;
}
set
{
this.seletectedItem = value;
if (!this.SelectedIsNull && this.SelectedChanged != null)
{
this.SelectedChanged(this, new EventArgs());
}
this.RaisePropertyChanged("SelectedItem");
}
}
/// <summary>
/// Gets the collection.
/// </summary>
public ReadOnlyObservableCollection<T> Collection { get; private set; }
/// <summary>
/// Gets the list collection.
/// </summary>
protected ObservableCollection<T> ListCollection { get; private set; }
/// <summary>
/// The add.
/// </summary>
/// <param name="item">
/// The item.
/// </param>
public void Add(T item)
{
this.ListCollection.Add(item);
}
/// <summary>
/// The clear.
/// </summary>
public void Clear()
{
this.ListCollection.Clear();
}
/// <summary>
/// The get index.
/// </summary>
/// <param name="predicate">
/// The predicate.
/// </param>
/// <returns>
/// The <see cref="int"/>.
/// </returns>
/// <exception cref="NullReferenceException">
/// </exception>
public int GetIndex(Func<T, bool> predicate)
{
if (predicate == null)
{
throw new NullReferenceException("predicate is null");
}
var index = this.ListCollection.Where(predicate)
.Select(m => this.ListCollection.IndexOf(m))
.SingleOrDefault();
return index;
}
/// <summary>
/// The get item.
/// </summary>
/// <param name="predicate">
/// The predicate.
/// </param>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
/// <exception cref="NullReferenceException">
/// </exception>
public T GetItem(Func<T, bool> predicate)
{
if (predicate == null)
{
throw new NullReferenceException("predicate is null");
}
var obj = this.ListCollection.SingleOrDefault(predicate);
return obj;
}
/// <summary>
/// The remove at.
/// </summary>
/// <param name="index">
/// The index.
/// </param>
public void RemoveAt(int index)
{
this.ListCollection.RemoveAt(index);
}
/// <summary>
/// The to list.
/// </summary>
/// <returns>
/// The <see cref="IList{T}"/>.
/// </returns>
public IList<T> ToList()
{
return this.ListCollection.ToList();
}
/// <summary>
/// The remove.
/// </summary>
/// <param name="item">
/// The item.
/// </param>
public void Remove(T item)
{
this.ListCollection.Remove(item);
}
/// <summary>
/// The on collection changed.
/// </summary>
/// <param name="e">
/// The e.
/// </param>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var handler = this.CollectionChanged;
if (handler != null)
{
handler(this, e);
}
}
}
<ComboBox
DataContext="{Binding FactoryRegulationCollectionVm, Mode=OneWay}"
ItemsSource="{Binding Collection}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
DisplayMemberPath="FactoryRegulationName" />
<DataGrid
Grid.Row="2" BorderBrush="#D24726" BorderThickness="1"
DataContext="{Binding ImageinfoCollectionViewModel}"
ItemsSource="{Binding Collection}"
SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
IsReadOnly="True" CanUserSortColumns="False" HorizontalAlignment="Center"
AutoGenerateColumns="False"
>
<i:Interaction.Triggers>
<!--双击事件绑定到命令-->
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction
Command="{Binding DoubleClickCommand}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="{DynamicResource lang_SerialNumber}" Binding="{Binding rowID}" />
</DataGrid.Columns>
</DataGrid>
3. Demo代码:CollectionViewModel