0

So I have a main windows that loads a page this page is a settings page and I have the setting topmost, because I am using a page to display the buttons I did:

if(TopMostCheckBox.IsChecked == true)
{
    MainWindow main = new MainWindow();
    main.Topmost = true;
}

if(TopMostCheckBox.IsChecked == false)
{
    MainWindow main = new MainWindow();
    main.Topmost = false;
}

but for some reason when I load my program I check box doesn't check top most so how can I make my page toggle top most for my main window.

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
MarkMent5
  • 11
  • 1

3 Answers3

1

Answering your Question title:

how can I make a topmost checkbox WPF C#

As a Minimal, Reproducible Example the following would apparently work in the MainWindow code-behind of a newly created WPF project:

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <CheckBox Checked="CheckBox_CheckedChanged"
                  Unchecked="CheckBox_CheckedChanged"/>
    </Grid>
</Window>

MainWindow.CS:

using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckBox_CheckedChanged(object sender, RoutedEventArgs e)
        {
            Topmost = ((CheckBox)sender).IsChecked == true;
        }
    }
}
Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
  • But my Checkbox is on a page and not my mainwindow and I'm trying to get the page checkbox to make the window form top most – MarkMent5 Aug 20 '22 at 02:08
  • As a wild guess, your code looks as if the second `if` could rather be an `else`, and as if you are having object-reference issues to find the existing instance of your `MainWindow` class, which probably is already running, as opposed to try to create `new()` MainWindow instances two times. – Stefan Wuebbe Aug 20 '22 at 07:35
  • See also https://stackoverflow.com/questions/17811547/wpf-c-window-page-and-events – Stefan Wuebbe Aug 20 '22 at 07:35
  • Difficult to tell what your problem is w/o having a _"Minimal, Reproducible Example"_ https://stackoverflow.com/help/minimal-reproducible-example – Stefan Wuebbe Aug 20 '22 at 07:37
  • See also https://stackoverflow.com/questions/48114746/wpf-binding-a-page-control-to-parent-windows-property – Stefan Wuebbe Aug 20 '22 at 07:41
0

You can do this within the xaml

<Window 
    .........
    Topmost="{Binding ElementName=chkbox, Path=IsChecked}" >
    <Grid>
       <CheckBox x:Name="chkbox" Content="topmost ?" />
    </Grid>
</Window>
Celso Lívero
  • 716
  • 2
  • 14
  • 18
-1

You're creating a new instance of mainwindow rather than referencing the existing instance.

        if (TopMostCheckBox.IsChecked == true)
        {
            var main = Application.Current.MainWindow as MainWindow;
            main.Topmost = true;
        }

The above assumes you've not given mainwindow a different name to the default. You might find you have to set topmost false and then true to get it to respond.

Andy
  • 11,864
  • 2
  • 17
  • 20