<DataTemplate DataType="{x:Type vm:TrollViewModel}">
<v:TrollView />
</DataTemplate>
protected void ShowWorkspace<T>() where T : WorkspaceViewModel, new()
{
WorkspaceViewModel workspace = this.Workspaces.FirstOrDefault(vm => vm is T) as T;
if (workspace == null)
{
workspace = new T();
this.Workspaces.Add(workspace);
}
this.SetActiveWorkspace(workspace);
}
protected void SetActiveWorkspace(WorkspaceViewModel workspace)
{
Debug.Assert(this.Workspaces.Contains(workspace));
ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
if (collectionView != null)
collectionView.MoveCurrentTo(workspace);
}
public static class GridCommands
{
public static readonly RoutedUICommand Delete =
new RoutedUICommand(
"Удалить строку",
"DeleteRow",
typeof (GridCommands),
new InputGestureCollection(new[] {new KeyGesture(Key.Delete)})
);
}
public class DeleteBinding : CommandBinding
{
public DeleteBinding()
{
Command = GridCommands.Delete;
CanExecute += DeleteBindingCanExecute;
Executed += DeleteBindingExecuted;
}
static void DeleteBindingExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (MessageBox.Show("Delete this?", "Delete this?", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
return;
ApplicationCommands.Delete.Execute(e.Parameter, (IInputElement)sender);
}
static void DeleteBindingCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = ApplicationCommands.Delete.CanExecute(e.Parameter, (IInputElement)sender);
}
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<DataGrid
Name="ItemsGrid"
ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding}"
Header="N"
Width="*"
IsReadOnly="True"/>
</DataGrid.Columns>
<DataGrid.CommandBindings>
<l:DeleteBinding />
</DataGrid.CommandBindings>
</DataGrid>
<Button
Grid.Row="1"
Padding="5"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Content="Delete Row"
Command="{x:Static l:GridCommands.Delete}"
CommandTarget="{Binding ElementName=ItemsGrid}" />
</Grid>
<DataGridComboBoxColumn Header="Колонна" Width="auto" SelectedValueBinding="{Binding Brigade}" DisplayMemberPath="Brigade1">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Brigades}"/>
<Setter Property="IsReadOnly" Value="True"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Brigades}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>