也许您应该尝试自定义模式对话框解决方案,也许使用DispatcherFrame
.
这是我为非常相似的解决方案(链接文本)提出的示例 - 运行示例,看看我的意思:
public partial class Window1 : Window
{
private DispatcherFrame frame;
private readonly ObservableCollection<string> collection = new ObservableCollection<string>();
public Window1()
{
InitializeComponent();
DataContext = collection;
}
private void GetData(object sender, RoutedEventArgs e)
{
collection.Clear();
frame = new DispatcherFrame();
popupGrid.Visibility = Visibility.Visible;
System.Windows.Threading.Dispatcher.PushFrame(frame); // blocks gui message pump & creates nested pump
var count = int.Parse(countText.Text); // after DispatcherFrame is cancelled, it continues
for (int i = 0; i < count; i++)
collection.Add("Test Data " + i);
popupGrid.Visibility = Visibility.Hidden;
}
private void DataCountEntered(object sender, RoutedEventArgs e)
{
frame.Continue = false; // un-blocks gui message pump
}
}
这是 XAML
<Grid>
<TabControl>
<TabItem Header="TabItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox ItemsSource="{Binding}"/>
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"
Content="Get Data" Grid.Row="1" Margin="0,5" Click="GetData"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid/>
</TabItem>
</TabControl>
<Grid Name="popupGrid" Visibility="Hidden">
<Grid.Background>
<SolidColorBrush Opacity="0.4" Color="#FFD8CFCF"/>
</Grid.Background>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="100"
BorderBrush="Black" BorderThickness="1" Background="White" Padding="5">
<StackPanel>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Enter Number Of Items"/>
<TextBox HorizontalAlignment="Left" Text="10" TextWrapping="Wrap" Margin="0,3"
Width="100" Name="countText"/>
<Button HorizontalAlignment="Left" Width="75" Content="Do Data Add" Click="DataCountEntered"/>
</StackPanel>
</Border>
</Grid>