Display VB.NET WPF window - wpf

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.

Related

Capturing name of StackPanel from Drop event

Within WPF I have the following XAML code:
<Page x:Class="com.MyCo.MyProj.Pages.Configuration.ManageLinkage.MasterLinkage"
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:com.MyCo.MyProj.Pages.Configuration.ManageLinkage"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="MasterLinkage">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TabControl TabStripPlacement="Top" Background="Transparent">
<TabItem Header="Import">
<ListBox Margin="0,5,0,0" Name="lbxImportItems" HorizontalAlignment="Left" VerticalAlignment="Top" Width="110" Background="Transparent"
PreviewMouseLeftButtonDown="lbxImportItems_PreviewMouseLeftButtonDown" >
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Name="DBImport">
<Image Source="/Images/DBImport25px.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
<TextBlock Text="Database" Foreground="AntiqueWhite"/>
</StackPanel>
<StackPanel Orientation="Vertical" Name="CSVImport">
<Image Source="/Images/CSVImport25px.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
<TextBlock Text="CSV Import" Foreground="AntiqueWhite"/>
</StackPanel>
</ListBox>
</TabItem>
</TabControl>
<Canvas x:Name="cnvsLinkScreen" AllowDrop="True" Grid.Column="1" Background="Transparent" Drop="cnvsLinkScreen_Drop" DragOver="cnvsLinkScreen_DragOver" ></Canvas>
</Grid>
The code for capturing the event is here:
private void cnvsLinkScreen_Drop(object sender, DragEventArgs e)
{
Canvas parent = (Canvas)sender;
object data = e.Data.GetData(typeof(string));
StackPanel objIn = (StackPanel)e.Data;
...
}
The drag and drop work great, the event method created the image in the canvas. However, I want to capture the Name="" from the StackPanels which are dropped.
I found the Name buried super deep in the "DragEventArgs e" object. I was think that there should be a way to cast the object (or the object within that object) as a StackPanel to easily work with it. The above code does not convert the StackPanel object( it's not at the root or the child object; I tried both) so it exceptions on "StackPanel objIn = (StackPanel)e.data;"
How do I either translate the incoming object to a StackPanel or how do I access the Name attribute from the Stackpanel?
I got it. I was close with the translation. To translate / typecast the object to what you are working with I needed to use the following line:
StackPanel objIn = (StackPanel)(e.Data.GetData(typeof(StackPanel)));
Which is slightly different than above.

Touch event in wpf interfering with other events

Help!
Been wrestling with a problem using Touch events in a wpf application I am developing.
Let me set the scene...
I have a main window (fmMainWindow) that has a Textbox on it. The window is coded to be centred to the screen when open. On the TouchUp event of the Textbox, I have code that opens up another window (fmKeypad_Numeric) using the syntax ShowDialog. This window is also centred. It is a numeric keypad with numbered buttons on it.
My problem is that the Touch Event used on the main window is having an affect on the Click events of the buttons on the window it opens.
It looks like wherever on the screen the Touch Event occurred, that the button on the next window opened at the same position seems to initially have some sort of 'focus' (it appears as if the mouse is hovered over it).
If you click the button that initially has the 'focus', the click event fires as expected.
If however you first click one of the other buttons that doesn't have the 'focus' its click event does not fire. It takes a second click for it to fire??
I have used Snoop to see how the events fire and when you click one of the other buttons that doesn't initially have the 'focus', Events related to the button that has the 'focus' fire??
Anyone got any ideas? I've spent days on this one.
Code for the two windows is below
Many thanks in advance,
Rhyd.
fmMainWindow
<Window x:Class="fmMainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<TextBox Name="tbTest" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="180,10,10,0" Width="150" Height="50" BorderBrush="#FF1B378B" Background="#FFEC1212"/>
</Grid>
Public Class fmMainWindow
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub tbTest_TouchUp(sender As Object, e As TouchEventArgs) Handles tbTest.TouchUp
e.Handled = True
Dim myKeypad As New fmKeypad_Numeric()
myKeypad.ShowDialog()
myKeypad.Close()
End Sub
End Class
fmKeypad_Numeric
<Window x:Class="fmKeypad_Numeric"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Keypad_Numeric" SizeToContent="WidthAndHeight" ResizeMode="NoResize" FontFamily="Century Gothic" FontSize="30" Left="10">
<Window.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Height" Value="70" />
</Style>
</Window.Resources>
<Grid Name ="grKeypad" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBox Name="tbNumber" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Right" Text="" Width="230" IsReadOnly="True" Margin="10,10,0,0"/>
<Button Name="bu7" Content="7" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,70,0,0" Width="70" />
<Button Name="bu8" Content="8" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,70,0,0" Width="70" />
<Button Name="bu9" Content="9" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,70,10,0" Width="70" />
<Button Name="bu4" Content="4" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,150,0,0" Width="70" />
<Button Name="bu5" Content="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,150,0,0" Width="70" />
<Button Name="bu6" Content="6" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,150,10,0" Width="70" />
<Button Name="bu1" Content="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,230,0,0" Width="70" />
<Button Name="bu2" Content="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="90,230,0,0" Width="70" />
<Button Name="bu3" Content="3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,230,10,0" Width="70" />
<Button Name="bu0" Content="0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,310,0,0" Width="70" />
</Grid>
Public Class fmKeypad_Numeric
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles bu0.Click, bu1.Click, bu2.Click, bu3.Click, bu4.Click, bu5.Click, bu6.Click, bu7.Click, bu8.Click, bu9.Click
Dim myButtonName As String
Dim myTextToAdd As String = ""
myButtonName = CType(sender, Button).Name
myTextToAdd = Replace(myButtonName, "bu", "")
Me.tbNumber.Text = Me.tbNumber.Text & myTextToAdd
End Sub
End Class

display content class xaml vb.net

Well i have a mainWindow written in xaml (vb.net) with 10 buttons and for each button i have a class (with buttons...) in my project. Example: when i click on button valid i display the content of the class valid in my grid. Each class is instantiated in the class mainWdow.
I also have a grid on this mainWindow.
When i click on a button in my mainWindow, i would like to display the content of the correspondig class in my grid and of course i want to be able to use this class (click on buttons...).
What's the best way to do this on vb.net / xaml please?
On this webpage http://msdn.microsoft.com/en-us/library/cc903947(v=vs.95).aspx there are some indications but it is not what i want because i don't want just only display the content on my class but i want to be able to work with the class displayed on my grid...
I give you thanks for your advices.
Ok, so first you should read up about DataTemplates from the Data Templating Overview page on MSDN. Now here is the basic principal... instead of putting your reusable XAML into Windows, declare them in DataTemplates in your Window.Resources section:
<DataTemplate x:Key="StopDataTemplate">
<!--Define your content here-->
</DataTemplate>
<DataTemplate x:Key="ValidDataTemplate">
<!--Define your content here-->
</DataTemplate>
Now you can display the content of these DataTemplates in a ContentControl:
<ContentControl Name="Content" ContentTemplate="{StaticResource ValidDataTemplate}" />
Then when you want to switch to the other view, you just need to set the ContentControl.ContentTemplate property to the other DataTemplate (from Window code behind):
DataTemplate dataTemplate = (DataTemplate)FindResource("StopDataTemplate");
Content.ContentTemplate = dataTemplate;
Well, here is what i have done to do some tests:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="CBTemplate">
<Grid Height="200" HorizontalAlignment="Left" Name="centralGrid" VerticalAlignment="Top" Width="200" Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{Binding Title}"
Margin="10" HorizontalAlignment="Left" FontSize="20"/>
</Grid>
</DataTemplate>
then in my mainWindow.xaml i have this:
<Grid x:Name="layout" Background="LightBlue" Margin="250,150,260,380">
<ContentControl Name="Content" ContentTemplate="{StaticResource CBTemplate}" />
</Grid>
And in the mainWindow.vb i have:
Class MainWindow
Private t As Thing
Sub New()
InitializeComponent()
t = New Thing("test")
layout.DataContext = t
End Sub
End Class
So my program compiles properly but there is a little problem: text="{Binding Title}" doesn't work and when i replace it with "toto", there is no problem toto is displayed...
Did i miss something in my code?

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();
}

Binding example

I have been trying for weeks to find a decent article that explains WPF Binding simply and with a working example that actually explains what is happening. Yes there are lots of examples out there but lots of those refer to older versions of everything involved.
I am trying to write an application, that is not web based, uses WPF and VB on VS2012.
In this bit of code i am updating a screen field directly, but i want to learn how to update a variable in memory and it update on the screen automatically, how do i get from here to there - In stages with explanations
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test Bind"
Height="299" Width="500" WindowStartupLocation="CenterScreen">
<DockPanel HorizontalAlignment="Stretch" LastChildFill="False" VerticalAlignment="Stretch">
<DockPanel DockPanel.Dock="Top" Height="64">
<Button Content="Button" Height="64" VerticalAlignment="Top" Width="75" Click="Button_Click" />
<TextBox Height="64" Width="120" Text="TextBox1" x:Name="clickcount" />
<TextBox Height="64" Width="120" x:Name="textBox2" Text="{Binding Test, Mode=Default}" />
</DockPanel>
</DockPanel>
</Window>
Class MainWindow
Public Shared clcount As Integer = 0
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
clcount = clcount + 1
Me.clickcount.Text = clcount.ToString
End Sub
End Class

Resources