I am developing a GUI application in Visual Studio with WPF. It seems that I've added too many things to my window and VS lags horrifically. It's also a bit difficult to work with too many elements on the screen.
A lot of my functionality is grouped together in their own tabs or panels. Isn't there a way to just work on one panel at a time, and then tell VS to include that in so and so part of the main window?
Break your functionality into UserControls, each with a specific purpose. Then group them together into more complex combinations of controls and then utlimately your application window.
I would investigate a UI pattern like Model-View-ViewModel or MVVM which fits nicely with WPF.
// MainWindow.xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame Name="EmbeddedView" Source="EmbeddedView.xaml"/>
</Grid>
</Window>
// EmbeddedView.xaml
<UserControl x:Class="EmbeddedView"
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">
Embedded Text
</UserControl>
Actually, more than just UserControls, you need the concept of Views and ViewModels - see Josh Smith's seminal article in MSDN Magazine:
WPF Apps With The Model-View-ViewModel Design Pattern
Eventually, you'll want to move on to something like the following, but understand Views and ViewModels first:
Caliburn.Micro: Screens, Conductors, and Composition
Related
I'm getting the XLS1106 "DataContext is not set" message on my main window XAML in Visual Studio. As far as I can tell, I haven't used anything related to that and nothing is broken. I would just supress the message, but I'm not sure why it's there.
Here is my whole XAML:
<Window x:Name="MyWindow" x:Class="Whiteboard.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:Whiteboard"
mc:Ignorable="d"
Title="Untitled - CFWhiteboard" Height="450" Width="800" MouseDown="WindowMouseDown" MouseMove="WindowMouseMove" KeyDown="WindowKeyDown" KeyUp="WindowKeyUp" Icon="icons8-interactive-whiteboard-100.png" Loaded="WindowLoaded" Closing="WindowClosing">
<Window.Resources>
<ResourceDictionary>
<FrameworkElement x:Key="CursorRectangle" Cursor="Resources/Cursors/rectangle.cur"/>
<FrameworkElement x:Key="CursorSquare" Cursor="Resources/Cursors/lockrect.cur"/>
<FrameworkElement x:Key="CursorEllipse" Cursor="Resources/Cursors/ellipse.cur"/>
<FrameworkElement x:Key="CursorCircle" Cursor="Resources/Cursors/lockellipse.cur"/>
</ResourceDictionary>
</Window.Resources>
<Canvas x:Name="MainCanvas"/>
</Window>
The message is a false positive. The fix for the problem is available in the latest preview version of Visual Studio:
Microsoft Solution - Bhavya Udayashankar [MSFT] Closed - Fixed ยทยทยท
A fix for this issue has been released! Install the most recent preview release from https://visualstudio.microsoft.com/downloads/. Thank you for providing valuable feedback which has helped improve the product.
See issue "XLS1106 on virgin C# WPF .Net application" on the Visual Studio Developer Community site.
In the simplest applications without any data bindings set up, there is no need for a data context to be set, and the warning given can be safely ignored.
Note that you should NOT follow the advice of the comment above telling you to add DataContext = this; to the window's constructor for real-world applications. Write a proper view model object type, and create an instance of that to set as your DataContext reference.
This question already has an answer here:
Can't put a window in a style (WPF)
(1 answer)
Closed 4 years ago.
I'm developing a small countdown app in WPF. My MainWindow should be used merely for using <ContentPresenter/> , but it doesn't seem to work- it comes up with the Cannot put Windows in Style error.
This has never happened before using this same approach.
My MainWindow:
<Window x:Class="CountdownTimer.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:CountdownTimer"
xmlns:viewmodels="clr-namespace:CountdownTimer.ViewModels"
xmlns:views="clr-namespace:CountdownTimer.Views"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<viewmodels:ApplicationViewModel/>
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type viewmodels:CountdownViewModel}">
<views:CountdownView/>
</DataTemplate>
</Window.Resources>
<ContentPresenter Content="{Binding CurrentView}"/>
CountdownView is the view containing the countdown UI. CountdownViewModel is the Viewmodel controlling the countdown system. ApplicationView defines CurrentView as a CountdownViewModel and sets it to an instance of CountdownViewModel in its constructor.
It's been solved. I was using an actual window instead of a user control. I set up a new view (user control) to test and it worked, so I double checked the view I wanted and it was actually a window.
Use usercontrols not windows for new views.
I made a WPF control in a library project and would like to extend it with a new one.
<UserControl x:Class="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI.EmbeddingUserControl"
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">
<Grid>
</Grid>
</UserControl>
I have tried to extend it like this:
<src:EmbeddingUserControl x:Class="Interaxalab.Desktop.Modules.PrototipoCable.CustomViews.InteractionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI"
Name="InteractionWorksheetView" Height="321.613" Width="471.396"
>
<Grid>
<WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="471"/>
</Grid>
</src:EmbeddingUserControl>
However, I get an error message saying that the name "EmbeddingUserControl" does not exist in namespace "Genesyslab.Desktop.Modules.ExtensionUtils85.GUI".
The name clearly does exist, since the xaml.cs can find it, but for some reason the xaml cannot.
Any help would be appreciated.
Long story short - you cannot inherit control with xaml by another control with xaml (and does it makes sense even to do so?). In your case, EmbeddingUserControl does not contain any visual tree elements (just empty grid), so you can just do:
public class EmbeddingUserControl : UserControl {
// some methods, properties of your control
}
Then you can inherit exactly like you do already in your question (don't forget to inherit from EmbeddingUserControl both in xaml file and in code-behind file).
You can also inherit from user control with xaml, if your inherited control does not have xaml itself (so you can add\override logic).
If you need to inherit some visual stuctures - you have to switch from inheritance to composition. That is: your base control provides some placeholders where other controls may be placed, or even better allows to provide templates to format data items (like for example ItemsControl and ItemTemplate property). Then you just fill those placeholders with other controls if necessary.
Chapter 10 of "WPF 4 Unleashed" by Adam Nathan includes this XAML example of controlling ListBox scrolling behaviour:
<Window x:Class="NathanControllingScrollingBehavior.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="False"
ScrollViewer.IsDeferredScrollingEnabled="True">
...
</ListBox>
</Window>
There isn't an equivalent C# example in the book. I did some research and found a few roundabout ways to do this. See this SO question for two approaches which worked for me. These approaches however, seem kind of hackish. Adjusting these properties is quite simple in XAML, but awkward from C#.
Is this simply an area of WPF which was meant to be used only from XAML and not C#? Can anyone explain the disparity in ease of use of these properties between XAML/C#? Is it just an oversight of the WPF team?
You can do it this way.
ListBox listBox1 = new ListBox();
listBox1.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty,
ScrollBarVisibility.Disabled);
listBox1.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty,
ScrollBarVisibility.Disabled);
listBox1.SetValue(ScrollViewer.CanContentScrollProperty, false);
listBox1.SetValue(ScrollViewer.IsDeferredScrollingEnabledProperty, true);
I am rewriting an old application built in FoxPro and my client doesnt want any change in GUI so I have to make an old DOS style GUI application using wpf, and here is where my problem starts.
The older application was a full screen application so here too I have to make it full screen no task bars..nothing. Now I changed the properties and everything was working very fine in my first window. but as soon as I use window2.showdialog() to invoke my second window, which too should go full screen BOOM...despite of the fact that I am using identical settings second window leaves some space at bottem (though it is border less and all) now I dont know whats going wrong here....
Here is my first window which works perfectly:
<Window x:Class="WpfAppMT.MainWindow"
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:mt="clr-namespace:WpfAppMT"
Title="MT" WindowStyle="None" WindowState="Maximized" ResizeMode="CanResize" SizeToContent="WidthAndHeight" Topmost="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="640" d:DesignWidth="480">
<Viewbox Stretch="Fill">.....</Viewbox></Window>
here is the screens shot of the first window which is perfectly fine:
and this is the xaml of my second window, which when invoked from the first window's event handler is always cut short at bottom
<Window
x:Class="WpfAppMT.accountheads"
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"
Title="accountheads" Background="#FF008284" WindowStyle="None" WindowState="Maximized" ResizeMode="CanResize" SizeToContent="WidthAndHeight" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="640" d:DesignWidth="480" KeyDown="Window_KeyDown">
.....</Window>
Here you can see that the second window is not full screen but leaves a lot of space at the bottom I dont understand the reason behind this peculiar behavior...
Apart from that I am facing a peculiar problem the GUI of application looks different on different OS's...I mean I am devolping it on an XP machine where it looks differently(the positions/length/widths), and today when I tested it on windows 7, there is lot of difference in look. You can see that in the second window text box which was supposed to be at the bottom has come up
OK..so after some trial and error I found the solution, In this case problem was with SizeToContent property I have set it to WidthandHeight but it should be set to Manual, so this answers the full screen question but still the prolem with portability persists