Can't add my custom user control in main windows WPF - c#

I can't add My user control to MainWindow.xaml
This is my solution explorer : (pointer show my user Control)
This is my user control header xaml file
<UserControl x:Class="SIManager.ucAddDevices"
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:SIManager"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500" FlowDirection="RightToLeft" FontFamily="B Nazanin">
And this is MainWindows Header Xaml
<Window x:Class="SIManager.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:SIManager"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
And now I can`t add my user control to tab item ... Why ?
In picture below My user control name wont appear ... !!!!
how I can add my user control to Tab Item ????

My user control Constructor gets params and won't appear in XAML.
I removed params from the user control Constructor and rebuilt the solution and it worked.
public ucAddDevices(MainWin win)
{
InitializeComponent();
mainWindow = window;
}
To:
public ucAddDevices() => InitializeComponent();

Related

how to stop right click on TtitleBar in a WPF application

Writing a program using WPF (with MahApps) In C# and wondering how can I disable a user being able to right click the TitleBar and brining up the window options menu?
Edit1: to confirm I would like to keep the min/max/close button on the bar, just remove the right click event
There is a new property in MahApps v2.x called ShowSystemMenuOnRightClick.
<mah:MetroWindow x:Class="MetroDemo.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:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MahApps.Metro - Demo Application"
Width="900"
Height="600"
mah:DialogParticipation.Register="{Binding}"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ShowIconOnTitleBar="True"
ShowSystemMenuOnRightClick="False"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
</mah:MetroWindow>

How to Load a WPF window in the MainWindow as a TabControl item?

The structure of my MainWindow.xaml is as bellow
<Window>
<Grid>
<TabControl Name="MainTabCntrl">
<TabItem1>
<TabItem2>
<TabItem3>
.
.
.
<TabItemN>
</TabControl>
</Grid>
</Window>
The problem is that my MainWindow.xaml is currently ~4000 lines of code,which is not efficient(do you agree?)
the solution I'm trying to implement is to create N windows (representing my TabItems) separately and each time user click on Tab items I load the related windows in that TabItem as shown bellow
private void inventory_start()//this function is called in my MainWinodw.xaml.cs
{
inv = new Inventory(db, logViewer);//this is a window
TabItem tbItem = new TabItem();
Frame frame = new Frame();
frame.Content = inv;
tbItem.Name = "invTab";
tbItem.Content = frame;
tbItem.IsSelected = true;
MainTabCntrl.Items.Add(tbItem);
inv.swithInventoryTabs("inv_info");
}
I have an error now , "'Management_V0.Inventory' root element is not valid for navigation."
A window cannot be a child of another element. Period.
But you could just move the contents of the Inventory window to a UserControl (by for example simply copy and paste the XAML and the code from one file to another) and use this one as the Content of the Inventory window and the Frame:
<Window x:Class="WpfApplication1.Inventory"
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:WpfApplication1"
mc:Ignorable="d"
Title="Inventory" Height="300" Width="300">
<local:UserControl1 />
</Window>
Frame frame = new Frame();
frame.Content = new UserControl1();
best way to use tabControl is with User control: Example below:
XAML
<TabControl >
<TabItem x:Name="tab1" Header="UserControl"></TabItem>
<TabItem x:Name="tab2" Header="noControl"></TabItem>
</TabControl>
and in code behind class:
tab1.Content = new UserControl1();
and than Add new user control of name UserControl1:
<UserControl x:Class="WpfApplication1.UserControl1"
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>
<Viewbox>
<TextBlock Text="User Control"/>
</Viewbox>
</Grid>
</UserControl>

Grid control can't cover the whole page and leaves a space at the top

I have a default grid control defined in xaml:
<Page
x:Class="App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
</Grid>
</Page>
And this grid does not start from the top of the page as you can see from the picture below:
One way to achieve this is to set a negative margin like this:
<Page
x:Class="App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="0,-26.667,0,0">
</Grid>
</Page>
So it will look like in this picture:
However, I've watched some of Bob Tabor's videos like this one and all of his default controls starts right from the top of the page.
What is the problem here and how can I solve it without setting a negative margin?
Well status bar is there by default.
You have 2 options:
Hide it -> Hide Status bar in Windows Phone 8.1 Universal Apps
put your content under it:
var applicationView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
applicationView.SetDesiredBoundsMode(Windows.UI.ViewManagement.ApplicationViewBoundsMode.UseCoreWindow);
You may wish to read this for additional info:
http://blogs.msdn.com/b/amar/archive/2014/05/12/status-bar-in-windows-phone-8-1.aspx

WPF Window not having title named correctly

I have a WPF application that has the title set to "Pioneer Document Handling Viewer" within the xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mt="clr-namespace:DocumentHandlingTouch" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="DocumentHandlingTouch.MainWindow"
Title="Pioneer Document Handling Viewer" Height="909" Width="1020" WindowState="Maximized"
Icon="default_document.ico" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
But it shows up with an older title: when I run it.
I've tried a clean rebuild. Reopening the project, etc and the title still doesn't change.
Why would the XAML be different than the runtime title?

Bringing a user control to the front in Expression Blend, C#, WPF

I have a window with a Grid on.
On this I have some buttons, one of which when clicked will create a new 'PostIt' which is a user control I have created.
What I want to do is click on a 'PostIt' and have that control on top of all the others.
I have tried...
Grid.SetZIndex(sender, value);
Which seems to be the correct code, no errors, just not movement of the control :(
The problem may lie in the fact that the code for the click is in the user control and not the mainwindow cs file. Does this matter?
The 'PostIt' is simply a border with a text box in it.
Are you calling Grid.SetZIndex(sender, value) in a handler of the PostIt mouse click, or a handler for a control inside the PostIt? What is the value that you are setting?
Here is an example that works:
<UserControl x:Class="WpfApplication1.UserControl1"
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" MouseUp="UserControl_MouseUp">
<Grid>
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
{
Panel.SetZIndex(this, Panel.GetZIndex(this) + 2);
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 Background="Green" Margin="40,40,100,100" Panel.ZIndex="0" />
<local:UserControl1 Background="Red" Margin="140,140,10,10" Panel.ZIndex="1" />
</Grid>
</Window>
Jogy
This may not be the best solution, but it's the one that worked for me; I was re-ordering two grids:
GridOnBottom.SetValue(Grid.ZIndexProperty, (int)GridOnTop.GetValue(Grid.ZIndexProperty) + 1);
...with GridOnBottom and GridOnTop renamed to the instances of the objects you're re-ordering. Granted, it's not the best solution, but it works.

Categories