Page can have only Window or Frame as parent - wpf

I just want to switch between WPF pages, but getting above mentioned error:Page can have only Window or Frame as parent.
here is my code
Imports System.Windows.Forms
Public Class Page1
Private Sub btn1_Click(sender As Object, e As RoutedEventArgs) Handles btn1.Click
Try
Dim dd As New Page2
Me.Content = dd
Me.txt1.Text = (dd.txt10.Text)
Catch ex As Exception
txt1.Text = ex.Message
End Try
xaml file code
<Page x:Class="Page2"
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="Page2">
<Grid>
<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="163,62,0,0" VerticalAlignment="Top" Width="101" Height="55"/>
<TextBox x:Name="txt10" HorizontalAlignment="Left" Height="23" Margin="38,81,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
please guide me what i am doing wrong?

Based on the name Page1 I assume it is a Page. Put a Frame in Page1 and load Page2 into the Frame.

Related

In WPF app the filepath is not showing from right end in textbox

I have a textbox in WPF window to show the path of file and browse button beside the textbox, I am selecting a file from (tfs) browser and I want to show the full path aligned from right side but it is showing from left side only.
The Image show the path showed from left
I tried HorizontalAlignment,VerticalAlignment,HorizontalContentAlignment but nothing is helping.
I want to display it from right end.
I have referred
Show right end of text line in TextBox
TextBox.TextAlign right-side alignment has no effect in certain conditions?
Can anyone Help?
Here is the code
<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>
<Label x:Name="label" Content="Source :
" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="1.144,1.871" Margin="10,27,0,0"/>
<TextBox x:Name="txtSourceFile" HorizontalAlignment="Left" Height="23" Margin="67,34,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="400" TextAlignment="Justify" HorizontalContentAlignment="Right" />
<Button x:Name="btnBrowseSource" Content="Browse" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="379,62,0,0" Click="btnBrowseSource_Click" />
</Grid>
</Window>
OK, so its not that trivial as setting a property on text box what you can do is declare this method:
private void ScrollToEnd()
{
var sv = (ScrollViewer)txtSourceFile.Template.FindName("PART_ContentHost", txtSourceFile);
sv.ScrollToHorizontalOffset(sv.ExtentWidth);
}
And call it whenever your textbox value is changed.
I had the same problem and added a method for the Loaded and TextChanged event
<TextBox Loaded="FolderTB_ScrolltoEnd" TextChanged="FolderTB_ScrolltoEnd" ... />
and for setting the offset
private void FolderTB_ScrolltoEnd(object sender, EventArgs e)
{
((TextBox)sender).ScrollToHorizontalOffset(((TextBox)sender).ExtentWidth);
}

control multiple XAML file through one code

I have made 2 XAML files.
The first one is loaded when I open the program and it contains a button.
XAML (WpfApplication4.xaml):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication4" Height="300" Width="300">
<Grid>
<Button x:Name="Button" Content="Button" HorizontalAlignment="Left" Height="86" Margin="47,68,0,0" VerticalAlignment="Top" Width="184" Click="Button_Click"/>
</Grid>
</Window>
The second XAML (called addon.xaml) contains a button and a list.
XAML (addon.xaml):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="addon" Height="300" Width="300">
<Grid>
<Button x:Name="Button2" Content="Button" HorizontalAlignment="Left" Height="86" Margin="47,68,0,0" VerticalAlignment="Top" Width="184" Click="Button2_Click"/>
<ListBox HorizontalAlignment="Left" Height="59" Margin="168,177,0,0" VerticalAlignment="Top" Width="81"/>
<Button Content="Button" HorizontalAlignment="Left" Height="13" Margin="242,25,0,0" VerticalAlignment="Top" Width="23"/>
<Label Content="test" HorizontalAlignment="Left" Height="38" Margin="80,25,0,0" VerticalAlignment="Top" Width="99"/>
</Grid>
</Window>
My goal is to switch between the XAML files when I clicked the button in order to switch screens (now it contains only button and list but generally it should be a very different screen than the first one).
The .py looks as following: (EDITED)
import wpf
from System.Windows import Application, Window
class MyWindow(Window):
def __init__(self):
self.ui = wpf.LoadComponent(self, 'WpfApplication4.xaml')
def Button_Click(self, sender, e):
self.ui = wpf.LoadComponent(self, 'addon.xaml')
def Button2_Click(self, sender, e):
self.ui = wpf.LoadComponent(self, 'WpfApplication4.xaml')
if __name__ == '__main__':
Application().Run(MyWindow())
And it manage to switch between the XAML however, when I click button2 to return to the original XAML (WpfApplication4) It says that there is already exists a variable named button (which is obvious because I cant change the button name every time it is clicked)
Is there a way to "delete" the memory of "self" so it wont remember the variables from the first time it was opened?
Could not register named object. Cannot register duplicate name 'Button' in this scope.
Thank you

Set binding on button tooltip with a declared instance of a user control as source

I've added a user control (with labels and textboxes) to my WPF project shown here:
<UserControl x:Class="MyProject.myControl"
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="384" Width="543">
<Grid x:Name="_LabelServerURL">
<Label x:Name="_LabelServerUrl" Content="Server Url:" HorizontalAlignment="Left" Margin="60,21,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
<TextBox x:Name="_TextboxUrl" HorizontalAlignment="Left" Height="23" Margin="158,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Label x:Name="_LabelURLExample" Content="(https://promodel.com)" HorizontalAlignment="Left" Margin="398,21,0,0" VerticalAlignment="Top" Width="139"/>
</Grid>
</UserControl>
and have created an instance of that user control in my main window code behind.
public partial class MainWindow : Window
{
private myControl _settings = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
_ButtonEPSSettings.ToolTip = _epsSettings;
//SetButtonTootips();
}
In my main window I have a list of buttons, and I want to set their tooltip property (or bind the tooltip property) to the instance of my user control so that the tooltip dynamically updates when I add values to the textboxes in my user control. So far in my main window xaml I've been able to show my user control as the tooltip shown here:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls ="clr-namespace:MyProject" <!--Declare project namespace-->
Title="EPS Configuration Manager" Height="415" Width="766">
<Grid>
<Button x:Name="_ButtonSettings"
Content="EPS Settings"
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="199"
HorizontalContentAlignment="Left"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="_ButtonSettings_Click"
>
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
I am still not clear on setting that tooltip as the instance of my user control as declared in my main window code behind. Any help will go a long way. Thanks.
If you set tooltips in backgroud, don't use these code:
<Button.ToolTip>
<ToolTip>
<StackPanel HorizontalAlignment="Left" Height="384" Margin="0,0,0,0" VerticalAlignment="Top" Width="543" Name="_StackPanelEPSSettings">
<Controls:myControl/>
</StackPanel>
</ToolTip>
</Button.ToolTip>
If you want the tooltip dynamically updates:
private myControl mytooltip = new myControl();
public MainWindow()
{
InitializeComponent();
AddStackPanelChildren();
mytooltip._TextboxUrl.Text = "Hello";
_ButtonEPSSettings.ToolTip = mytooltip;
//SetButtonTootips();
}

Display VB.NET WPF window

Sorry for this extremely amateur question, but I cannot make this work. I want to make a custom font dialog window (just for the heck of learning how it would be done) and from what I have found using Google, I should create an instance of the window I want to show and then call the Show() or ShowDialog() methods. However the intellisense popup does not show such methods as available and indeed the code does not compile and complains that those methods don't exist. Is there something really simple I am missing or am I just way off ?
Imports System.IO
Class MainWindow
Public font_dialog As Window1 = New Window1
// ... Removed code that was not pertinent
Private Sub menu_font_Click(sender As System.Object, e As _
System.Windows.RoutedEventArgs) Handles menu_font.Click
// does not compile
font_dialog.Show()
End Sub
End Class
Here is the exact error message:
Error 1 'Show' is not a member of
'WpfApplication1.Window1'. C:\Users\notmyrealusername\documents\visual studio
2010\Projects\WpfApplication2\WpfApplication2\MainWindow.xaml.vb 24 9 WpfApplication2
XAML for Window1:
<UserControl x:Class="Window1"
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" Height="453" Width="600" DataContext="{Binding}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="575*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TabControl Height="429" HorizontalAlignment="Left" Margin="12,12,0,0" Name="TabControl1" VerticalAlignment="Top" Width="576" Grid.ColumnSpan="2">
<TabItem Header="Paramètres généraux" Name="TabItem1">
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="53,14,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="213" />
<Label Content="Police" Height="28" HorizontalAlignment="Left" Margin="6,14,0,0" Name="Label1" VerticalAlignment="Top" />
<Label Content="Styles" Height="28" HorizontalAlignment="Left" Margin="6,43,0,0" Name="Label2" VerticalAlignment="Top" />
<ListBox Height="100" HorizontalAlignment="Left" Margin="53,43,0,0" Name="ListBox1" VerticalAlignment="Top" Width="213" SelectionMode="Multiple" />
</Grid>
</TabItem>
</TabControl>
</Grid>
</UserControl>
You must make Window1 inherit from Window, which has the Show method. In Visual Studio, you can right click the project you want to add the window to and click Add -> Window.
'Show' is not a member of 'WpfApplication1.Window1'.
That mans your Window1 is not a (valid) Window ...
Post the first lines of the XAML and the code behind.
Also, you probably want to call ShowDialog(), but that is a separate issue.

Object Reference not set error while trying to set the visiblity of grid

I was trying to set the visiblity of a Grid from code behind.
grdStopTimeOut.Visibility = Windows.Visibility.Visible
I have declared this grid in XAML and have set the visiblity to Visible.
<Grid Name="grdTimeTStopCondition" Visibility="Hidden" Margin="0,29,0,-6">
Somehow when the application runs the grid is coming as nothing and the exceptions is thrown.
Anybody have any idea why it is happening??
XAML File
<Page x:Class="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>
<ComboBox Height="23" HorizontalAlignment="Left" Name="cmbStopConditions" VerticalAlignment="Top" Width="86" SelectedIndex="0">
<ComboBoxItem Content="Expression"></ComboBoxItem>
<ComboBoxItem Content="Manual"></ComboBoxItem>
</ComboBox>
<Grid Name="grdStopTimeOut" Visibility="Visible" >
<Label Content="Timeout" Height="28" HorizontalAlignment="Left" Name="lblTimeout_stopcond" VerticalAlignment="Top" Margin="0,29,0,0" />
<TextBox Height="23" HorizontalAlignment="Left" Name="txtStopTimeout" VerticalAlignment="Top" Width="30" Margin="60,29,0,0" />
<Label Content="secs" Height="28" HorizontalAlignment="Left" Name="lblTimeoutSec_stopCond" VerticalAlignment="Top" Width="39" Margin="105,24,0,0" />
</Grid>
</Grid>
</Page>
CodeBehind
Class Page1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub cmbStopConditions_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbStopConditions.SelectionChanged
Dim item As ComboBoxItem = TryCast(cmbStopConditions.SelectedItem, ComboBoxItem)
If item IsNot Nothing Then
If Convert.ToString(item.Content) = "Expression" Then
grdStopTimeOut.Visibility = Windows.Visibility.Visible
ElseIf Convert.ToString(item.Content) = "Manual" Then
grdStopTimeOut.Visibility = Windows.Visibility.Hidden
End If
End If
End Sub
End Class
regards,
SKB
Edit: The handler can occur before the grid is initialized, so a null-check is in order.
Are you calling this before InitializeComponent? That for one would explain the lack of a reference because all the fields are hooked up with the named controls in that method.

Resources