如何打开对话框并将其放置在父 WPF 页面的右下角?

发布于 2024-10-04 03:25:38 字数 696 浏览 2 评论 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
    }

我应该使用哪种方法?

I have a base WPF Page (not Window) which has a Button. Now when I click on the Button, I have to open a modal dialog and place it at the bottom right part of parent(WPF Page).

I have created a modal dialog as follows ::

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

Now I am unable to figure how to place the dialog in the bottom right part of parent WPF Page. !!

EDIT 1 : I am trying these 2 methods in codebehind of CDialog but I am getting Parent as 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
    }

Which method should I use??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-10-11 03:25:38

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

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

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

Following code should do the trick:

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

NOTE: this code assumes that child has been rendered and thus Actual* properties have the correct values. So, place this code in a place where you know the child has been rendered/shown.

一城柳絮吹成雪 2024-10-11 03:25:38

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

Maybe you should try a custom modal dialog solution, perhaps using DispatcherFrame.

Here's an example I came up with for a very similar solution (link text) - run the example, and see what I mean:

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
    }
}

and this is the 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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文