开发者

How do I Open a dialog and place it at bottom right of Parent WPF Page?

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


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.


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜