0

我有一个基本的WPF 页面(不是窗口),它有一个按钮。现在,当我单击按钮时,我必须打开一个模式对话框并将其放在父级(WPF 页面)的右下角。

我创建了一个模态对话框,如下所示:

  CDialog dialog = new CDialog();
  dialog.ShowDialog();

现在我无法弄清楚如何将对话框放置在父 WPF 页面的右下部分。!!

编辑 1:我正在 CDialog 的代码隐藏中尝试这两种方法,但我将 Parent 设为 null!

   private void Window_Loaded(object sender, RoutedEventArgs e)
    {

         object obj = (sender as Window).Parent; //nullreference exception
    }

    private void Window_LayoutUpdated(object sender, EventArgs e)
    {
        object obj = (sender as Window).Parent; //nullreference exception
    }

我应该使用哪种方法??

4

2 回答 2

0

也许您应该尝试自定义模式对话框解决方案,也许使用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>

于 2010-11-24T11:27:22.083 回答
0

以下代码应该可以解决问题:

child.Left = parent.ActualWidth - child.ActualWidth;
child.Top = parent.ActualHeight - child.ActualHeight;

注意:此代码假定 child 已被渲染,因此 Actual* 属性具有正确的值。因此,将此代码放在您知道孩子已被渲染/显示的地方。

于 2010-11-24T11:18:41.943 回答