我是PRISM和WPF的新手,对我的英语也很抱歉。 我的问题如下: 我有一个数据网格,当用户想删除行时,我通过确认弹出窗口(棱镜)通知他/她。如果他/她接受,则将删除所选行,但如果他/她取消该过程,则在关闭此窗口后,数据网格不会恢复其焦点。 所选记录仅为灰色而非蓝色,如默认情况下。
取消弹出后。这不好! Image1
一定是这样! Image2
这是我的ViewModel:
/// <summary>
/// DataManipulationViewModel
/// </summary>
public class DataManipulationViewModel : BindableBase
{
private ObservableCollection<PriceCalcDS.BUTORLAPRow> collectionButorlap;
private ObservableCollection<PriceCalcDS.BUTORLAPRow> currentButorlap;
private ICollectionView colViewButorlap;
private readonly ICommand deleteCommandButorlap;
private Confirmation confirmation;
/// <summary>
/// Confirmation to deleting records
/// </summary>
public InteractionRequest<IConfirmation> ConfirmationRequestDeletingRecords { get; private set; }
/// <summary>
/// Current BUTORLAP
/// </summary>
public ObservableCollection<PriceCalcDS.BUTORLAPRow> CurrentButorlap
{
get { return currentButorlap; }
set
{
this.SetProperty(ref this.currentButorlap, value);
}
}
/// <summary>
/// The rows of the table BUTORLAP
/// </summary>
public ICollectionView ColViewButorlap
{
get { return colViewButorlap; }
set
{
this.SetProperty(ref this.colViewButorlap, value);
}
}
/// <summary>
/// Delete command
/// </summary>
public ICommand DeleteCommandButorlap
{
get { return deleteCommandButorlap; }
}
/// <summary>
/// Constructor
/// </summary>
public DataManipulationViewModel()
{
DataBase.FillDataSet();
this.collectionButorlap = new ObservableCollection<PriceCalcDS.BUTORLAPRow>();
this.currentButorlap = new ObservableCollection<PriceCalcDS.BUTORLAPRow>();
FillDtButorlap();
this.colViewButorlap = new ListCollectionView(collectionButorlap);
this.colViewButorlap.CurrentChanged += SelectedButorlapChanged;
this.deleteCommandButorlap = new DelegateCommand<object>(this.DeleteRecordButorlap);
this.ConfirmationRequestDeletingRecords = new InteractionRequest<IConfirmation>();
this.confirmation = new Confirmation();
}
/// <summary>
/// SelectedChanged event
/// </summary>
private void SelectedButorlapChanged(object sender, EventArgs e)
{
// Deleteing previous values from currentButorlap
this.currentButorlap.Clear();
if (colViewButorlap.CurrentItem != null)
{
this.currentButorlap.Add(colViewButorlap.CurrentItem as PriceCalcDS.BUTORLAPRow);
}
}
/// <summary>
/// Fill collectionButorlap with the rows of the table BUTORLAP
/// </summary>
private void FillDtButorlap()
{
foreach (var item in StaticDataSet.priceCalcDs.BUTORLAP.Rows)
{
collectionButorlap.Add(item as PriceCalcDS.BUTORLAPRow);
}
}
/// <summary>
/// Deleting rows of the table BUTORLAP
/// </summary>
/// <param name="parameter">Deleting rows (selected in DataGrid)</param>
private void DeleteRecordButorlap(object parameter)
{
IList selection = (IList)parameter;
List<object> deletingRows = new List<object>();
if (selection.Count != 0)
{
// Save deleting rows
foreach (var selectionValue in selection)
{
deletingRows.Add(selectionValue);
}
// Notica init
this.confirmation.Title = "Figyelmeztetés";
if (deletingRows.Count == 1)
{
this.confirmation.Content = "Biztosan törli a kijelölt rekordot?";
}
else
{
this.confirmation.Content = "Biztosan törli a kijelölt rekordokat?";
}
// Notify user
this.ConfirmationRequestDeletingRecords.Raise(
this.confirmation,
c =>
{
if (c.Confirmed)
{
// If user accpted request, the rows will be deleted
foreach (var deletingRow in deletingRows)
{
collectionButorlap.Remove(deletingRow as PriceCalcDS.BUTORLAPRow);
}
}
else
{
//If user cancelled request
}
});
}
}
}
这是我的XAML:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://www.codeplex.com/prism"
xmlns:support="clr-namespace:Support"
xmlns:PriceCalcDSTableAdapters="clr-namespace:PriceCalculator.Common.Dataset.PriceCalcDSTableAdapters;assembly=PriceCalculator.Common" x:Name="ucDataManipulation" x:Class="PriceCalculator.Modules.DataManipulation.Views.DataManipulationView"
mc:Ignorable="d" d:DesignWidth="1365" d:DesignHeight="750" Height="Auto" Width="Auto">
<UserControl.Resources>
<Color x:Key="TitleBgColor">#FF183B3F</Color>
<Color x:Key="TitleColor">#FF07333A</Color>
<Color x:Key="ButtonColor">#FF3E4949</Color>
</UserControl.Resources>
<UserControl.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FF022F36"/>
<GradientStop Offset="0.994" Color="{DynamicResource ViewBaseColor}"/>
</LinearGradientBrush>
</UserControl.Background>
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequestDeletingRecords, Mode=OneWay}">
<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
<Grid x:Name="gridDataManipulation" Cursor="Arrow" Focusable="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="8*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="165*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
<RowDefinition Height="16"/>
<RowDefinition Height="24"/>
<RowDefinition Height="23"/>
<RowDefinition Height="12"/>
</Grid.RowDefinitions>
<Border x:Name="brdFrame" BorderThickness="2" Padding="0" Margin="0,10,0,0" Grid.RowSpan="5" Grid.Column="1" Grid.ColumnSpan="9">
<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource FrameColor}"/>
</Border.BorderBrush>
</Border>
<TextBlock x:Name="txbTitle" TextWrapping="Wrap" Text="ADATMANIPULÁCIÓ" Margin="0,1,0,0" FontSize="16" FontFamily="Microsoft Sans Serif" FontWeight="Bold" TextAlignment="Center" Foreground="White" Height="20" Width="160" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="2" Grid.ColumnSpan="3">
<TextBlock.Background>
<SolidColorBrush Color="{DynamicResource TitleColor}"/>
</TextBlock.Background>
</TextBlock>
<TabControl x:Name="tcDataManipulation" TabStripPlacement="Top" Margin="0,15,0,0" Grid.Row="1" Grid.Column="2" BorderBrush="White" Grid.ColumnSpan="7">
<TabControl.Background>
<SolidColorBrush Color="{DynamicResource FrameColor}"/>
</TabControl.Background>
<TabItem Header="BÚTORLAP" Margin="0" Padding="10,2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" FontSize="13.333" BorderThickness="2">
<TabItem.BorderBrush>
<SolidColorBrush Color="{DynamicResource ButtonColor}"/>
</TabItem.BorderBrush>
<TabItem.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0F0" Offset="0"/>
<GradientStop Color="{DynamicResource FrameColor}" Offset="1"/>
</LinearGradientBrush>
</TabItem.Background>
<Grid x:Name="gridButorlap" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35*"/>
<ColumnDefinition Width="57"/>
<ColumnDefinition Width="66"/>
<ColumnDefinition Width="129"/>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="195"/>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="501*"/>
<ColumnDefinition Width="36*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="16*"/>
<RowDefinition Height="397*"/>
<RowDefinition Height="53*"/>
<RowDefinition Height="27"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="36"/>
<RowDefinition Height="16*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="dgButorlap" ItemsSource="{Binding Path=ColViewButorlap, Mode=TwoWay}" EnableRowVirtualization="True" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" AutoGenerateColumns="False" Margin="5,0,5,10" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="9">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="support:DataGridRowBehavior.IsDataGridRowFocussedWhenSelected" Value="true"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="NÉV" Binding="{Binding Path=BL_NEV}"/>
<DataGridTextColumn Header="EGYSÉG" Binding="{Binding Path=BL_EGYSEG}"/>
<DataGridTextColumn Header="ÁR" Binding="{Binding Path=BL_AR}"/>
<DataGridTextColumn Header="HOSSZÚSÁG" Binding="{Binding Path=BL_HOSSZ}"/>
<DataGridTextColumn Header="SZÉLESSÉG" Binding="{Binding Path=BL_SZEL}"/>
<DataGridTextColumn Header="KIHOZATAL" Binding="{Binding Path=BL_KIHOZATAL}"/>
</DataGrid.Columns>
</DataGrid>
<Label Content="Felvitel/Módosítás" Margin="5,1,9,5" Foreground="White" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.Row="3" Padding="0" Grid.Column="1" Grid.ColumnSpan="2"/>
<DataGrid x:Name="dgBlFelvitelModositas" ItemsSource="{Binding Path=CurrentButorlap}" CanUserAddRows="False" AutoGenerateColumns="False" Margin="5,3,5,0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="4" Grid.Column="1" Grid.RowSpan="3" Grid.ColumnSpan="9">
<DataGrid.Columns>
<DataGridTextColumn Header="NÉV" Binding="{Binding Path=BL_NEV}"/>
<DataGridTextColumn Header="EGYSÉG" Binding="{Binding Path=BL_EGYSEG}"/>
<DataGridTextColumn Header="ÁR" Binding="{Binding Path=BL_AR}"/>
<DataGridTextColumn Header="HOSSZÚSÁG" Binding="{Binding Path=BL_HOSSZ}"/>
<DataGridTextColumn Header="SZÉLESSÉG" Binding="{Binding Path=BL_SZEL}"/>
<DataGridTextColumn Header="KIHOZATAL" Binding="{Binding Path=BL_KIHOZATAL}"/>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="btnBLNewRecord" Content="ÚJ FELVITELE" Style="{DynamicResource btnStyle}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Foreground="White" Grid.Column="1" Grid.ColumnSpan="2" Margin="5,16,18,0"/>
<Button x:Name="btnBLModifyRecord" Content="MÓDOSÍTÁS" Style="{DynamicResource btnStyle}" Command="{Binding ModifyCommand}" CommandParameter="{Binding ElementName=dgBlFelvitelModositas, Path=SelectedItems}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Grid.Column="3" Foreground="White" Margin="0,16,29,0"/>
<Button x:Name="btnBLDeleteRecord" Content="TÖRLÉS" Style="{DynamicResource btnStyle}" Command="{Binding DeleteCommandButorlap}" CommandParameter="{Binding ElementName=dgButorlap, Path=SelectedItems}" RenderTransformOrigin="0.5,0.5" Grid.Row="7" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="13.333" Grid.Column="4" Foreground="White" Grid.ColumnSpan="2" Margin="0,16,50,0"/>
</Grid>
</TabItem>
请帮助我,我该如何解决这个问题? 我正在使用MVVM模式,如果解决方案是在xaml或ViewModel中,那将会很棒。
答案 0 :(得分:0)
你应该把你的元素重点放回去(因为你的对话偷走了它:))。
所有你需要做的,就是当他取消,得到你的对象,然后把焦点放回去时:
Keyboard.Focus(object_you_want_focused);
您可以在MSDN上找到更多信息(如果它显示&#34;不可用&#34;,将版本更改为.Net 4,您就会看到它......)