I have a WPF window that is inherited from custom window. It runs correctly but VS designer shows it like default empty window instead.
Custom window code:
using System.Windows;
using System.Windows.Media;
namespace WpfApplication2
{
public class CustomWindow : Window
{
public CustomWindow()
{
Background = new SolidColorBrush(Colors.Red);
}
}
}
MainWindow.xaml:
<local:CustomWindow x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
</local:CustomWindow>
Designer:
Expected result:
The XAML designer in both Visual Studio and Blend doesn't necessarily respect any dependency properties that are set directly in code (especially in the constructor), but rather expects those to be set in XAML only, which is the canonical way of working with the dependency system at design time. Of course all things can be set in code if you want, but when using the XAML designer you cannot guarantee the same behaviour as you will see in a running application.
Also, if you are setting dependency properties in code, then you need to be very careful about how you set them, as you may destroy any bindings that may end up being set on those properties.
Anyway, a better way of getting the result you want is to set the color in the XAML like this
<local:CustomWindow x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525" Background="Red">
</local:CustomWindow>
Related
I want to Edit UI XAML files with my program running and see these changes without to stop my program...
When I change these files right now It just o nothing until I close my program and start again.
For exmaple:
this is my xaml code:
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
http://prntscr.com/nwiw0o
I see an empty white screen
When I change it (on debug) to
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Background="Blue">
<Grid>
</Grid>
</Window>
http://prntscr.com/nwiw7q
Nothing is change
But when I restart my program
It runs this blue screen...
http://prntscr.com/nwiwiw
I think its something about debugging option?
this is my page open list
http://prntscr.com/nwiwtu
You probably don't have "Edit and continue" enabled, because you can just edit xaml and see the result immediately if you do.
From the Debug menu, choose Options.
On the left Debugging should be selected for you and the default will be the first option - General. If that isn't selected, do so.
Scroll the right panel down.
Tick the Enable UI debugging tools for XAML and at least Enable XAML Edit and Continue.
You should then be able to change simple properties in XAML and see your changes reflected immediately in the running UI. It does not work for everything though.
https://devblogs.microsoft.com/visualstudio/ui-development-made-easier-with-xaml-edit-continue/
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.
having some troubles with UserControl. I cannot get rid of "Content is set more than once error"
From what I've read, the common cause is that .. well, content is set more than once. For example having more than one child or setting content via Content=".." and then specifying another content between the tags.
However, this is UserControl generated by VisualStudio and I made no changes to the xaml.
<UserControl x:Class="TMEGadget.View.Toolbox"
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>
Got any tips?
P.S: Can anyone tell me why, when I try to type "Hey,\n\nSome text...", the "Hey,\n\n" is deleted?
Edit: Restarting VS fixed the problem , thanks #Bolu
I have a created a usercontrol in my wpf application. I have used it like so:
<Page x:Class="InstallerToolkit.Pages.PageVideosNvr"
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:my="clr-namespace:MyProject.UserControls"
mc:Ignorable="d"
d:DesignHeight="525" d:DesignWidth="1050"
Title="PageVideos">
<Grid>
<my:UserControlVideoPlayer Name="VideoPlayer" Margin="559,74,35,155">
</my:UserControlVideoPlayer>
</Grid>
Now in my C# page I want to access it but the VideoPlayer object doesnt appear when I type its name in the code behind c# page.
What doo I do to access it as I want to set one of its properties.
Give x:Name to your UserControl instead of Name
<my:UserControlVideoPlayer x:Name="VideoPlayer" Margin="559,74,35,155">
It will be accessible now in code behind using this.VideoPlayer.
I would suggest to make it a thumb rule that always use x:Name whenever referring to elements in XAML.
Refer to this for difference between Name and x:Name.
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