This question already has answers here:
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
(3 answers)
Closed 4 years ago.
I have a main window that I created and it looks like usual log in screen.
Once the user click on a button I would like the whole window to change.
In all the examples I saw either I have to keep a stackpanel/dock and then only the frame changes.
For example, I have the following Main window 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="350" Width="525">
<Grid>
<Frame x:Name="Main" Margin="0,35,0,0"/>
<Button Content="Button" Click="Button_Click" Margin="0,105,326,100"/>
</Grid>
</Window>
and the new page:
<Page x:Class="WpfApp1.Optimizer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Optimizer">
<Grid Background="Black">
<Button Content="Button" HorizontalAlignment="Left" Height="91" Margin="125,87,0,0" VerticalAlignment="Top" Width="129"/>
</Grid>
</Page>
And my command is:
private void Button_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Optimizer();
}
In that case the two views appear on same page instead of switching completely.
Its not the case im looking for. I need the whole window to change and not leave behind a stackpanel.
Any simple example would be appreciated.
First thing to mention here is that pages and frames are rarely used by commercial teams. At least not in my experience.
Unless writing xbap - which is wpf in a browser.
I suggest you consider usercontrols rather than pages and host them in contentcontrols.
You can then have something like:
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="Button_Click"/>
</Grid>
</Window>
Click handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Content = new UserControl1();
}
That makes the entire content of mainwindow an instance of UserControl1.
A Window inherits from contentcontrol.
You should also have learning mvvm on your list.
You should learn that. Maybe not right now, but soon.
Related
This question already has answers here:
Why does WPF support binding to properties of an object, but not fields?
(2 answers)
using of INotifyPropertyChanged
(3 answers)
Closed 11 months ago.
i'm struggling to get a very simple mvvm app off the ground and not sure why,
imagine an app such as the below:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow app = new MainWindow();
MainViewModel context = new MainViewModel();
app.DataContext = context;
app.Show();
}
}
And a mainViewModel that looks like this:
class MainViewModel
{
public object CurrentViewModel;
public MainViewModel()
{
CurrentViewModel = new MenuViewModel();
}
}
and the MainWindow like so:
<Window x:Class="myApp.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:myApp"
mc:Ignorable="d"
Title="MainWindow" Height="460" Width="800">
<Window.Resources>
<DataTemplate DataType="{x:Type local:MenuViewModel}">
<local:Menu />
</DataTemplate>
</Window.Resources>
<ContentControl
Content="{Binding CurrentViewModel}"
HorizontalAlignment="Left"
VerticalAlignment="Top" Height="440" Width="790" Margin="0,0,0,-21"/>
</Window>
with the menu Page View like this:
<UserControl x:Class="myApp.Menu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:myApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Content="MyButton" HorizontalAlignment="Left" Margin="34,90,0,0"
VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
So basically, when the application loads i would like to show the menu View, but the code
CurrentViewModel = new MenuViewModel();
inside MainViewMode is never hit,
i am assuming that is why the content control is never populated with the Menu View and why the button inside the Menu is never displayed,
i can't understand why this point is never reached, what fundamentally hilarious thing am i missing?
I am experimenting with the Zoombox control from Xceed, but I am having trouble getting it to respond to mouse wheel or pan events. Nothing happens when I use these inputs. Am I missing something in my code or configuration?
https://github.com/xceedsoftware/wpftoolkit
<Window x:Class="UI.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:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="700">
<Grid>
<xctk:Zoombox MinScale="0.5" MaxScale="100" >
<Grid Width="600" Height="400" Background="Yellow">
<Ellipse Fill="Blue" />
</Grid>
</xctk:Zoombox>
</Grid>
</Window>
You have to define DragModifiers and ZoomModifiers. Default values are Ctrl and Shift keys. So use the combination Shift+MouseWheel for zooming and use Ctrl+LeftButton for panning.
I want to display a progress bar in WPF Page,when it loaded. I used media element to display a gif image. Below code describe what I did.
<Page x:Class="WpfApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1" Loaded="Page_Loaded">
<Grid>
<MediaElement Grid.Column="2" Grid.Row="4" Margin="0,0,47,0" HorizontalAlignment="Right" Width="80" Source="progress.gif" LoadedBehavior="Manual" Name="MP"/>
</Grid>
</Page>
private void Page_Loaded(object sender, RoutedEventArgs e)
{
MP.Play();
}
but this progress bar not showing in Page
How can I fix this.
(when I tried with progress bar also, got same problm)
(It works ok in window )
You could use the WpfAnimatedGif library to easily display an animated gif using a simple Image element:
How do I get an animated gif to work in WPF?
Just download the package from NuGet: https://www.nuget.org/packages/WpfAnimatedGif
And set the ImageBehavior.AnimatedSource attached property of the Image element:
<Page x:Class="WpfApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1"
<Grid>
<Image gif:ImageBehavior.AnimatedSource="progress.gif" />
</Grid>
</Page>
I am a little new to WPF.
I want to use one window for all my content, the content will vary dramatically and the Window.Resources content will need to vary also.
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Grid>
...
</Grid>
</Window>
As an example, I would like to use some third party content like FluidKit's ElementFlow.
There are many good examples of how to dynamically Load UserControls, but nothing on Window in Window. The best I have found is: Changing content dynamically in wpf window
I would like to have multiple *.xaml Files in my project and load content dynamically into the Current Window.
EDIT:
I like WPF Frame, but I can not load and unload Window.Resources:
<Window x:Class="NS01.WPF01"
xmlns:b="clr-namespace:NS01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:FluidKit.Showcase.ElementFlow"
xmlns:Controls="clr-namespace:FluidKit.Controls;assembly=FluidKit"
Loaded="Window_Loaded"
Title="NS01"
WindowStartupLocation="CenterScreen"
Width="1280"
Height="720"
WindowStyle="ThreeDBorderWindow">
<Window.Resources>
...
</Window.Resources>
<Frame Name="ContentHolder" />
</Window>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
Just so you know, I have explored:
SolidColorBrush SolidColorBrushRed = new SolidColorBrush(Colors.Red);
this.Resources.Add("RedBrushResource", SolidColorBrushRed);
Is what I am wanting to do possible? Can I load all the required content from a single xaml File?
Thanks.
EDIT:
This is one method, but not really what I was wanting:
<Page x:Class="NS01.XAML.Repository.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NS01.XAML.Repository"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<Grid>
<TextBlock Text="Welcome" FontSize="18" Foreground="{DynamicResource ResourceKey=textForeColorResource}" Margin="80,32,66,219"/>
</Grid>
</Page>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NS01.XAML.Resources">
<SolidColorBrush x:Key="textForeColorResource" Color="Blue"/>
</ResourceDictionary>
ContentHolder.Source = new Uri("/XAML/Repository/Page1.xaml", UriKind.Relative);
ResourceDictionary RD = new ResourceDictionary()
{
Source = new Uri("/NS01;component/XAML/Resources/Page1.xaml", UriKind.RelativeOrAbsolute)
};
Application.Current.Resources.MergedDictionaries.Add(RD);
Application.Current.Resources.MergedDictionaries.Remove(RD);
foreach (var R in MWindow.Resources)
{
MessageBox.Show(R.ToString());
}
Is it possible to hide the default resize grip in wpf, I have a custom window without handles on the sides and are currently using:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="200" ResizeMode="CanResizeWithGrip">
<Grid></Grid>
</Window>
I know it's possible to just change ResizeMode="CanResize" but this is the only way to resize the window that I can think of.
Yes you can take out the default resize grip from the Window. You need to override the WindowTemplate. Here is how you would do in XAML:
<Window x:Class="MyNamespace.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
ResizeMode="CanResizeWithGrip"
AllowsTransparency="True">
<Window.Template>
<ControlTemplate>
<Border>
</Border>
</ControlTemplate>
</Window.Template>
</Window>
Within the "Border" will go everything that you need to display in the Window (e.g. a TextBox). Then if you want your custom ResizeGrip you can define that as well. Hope this helps.