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
Related
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.
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
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.
I have a popup that opens on the MainPage with a couple of textboxes. When ever you focus on the lower textbox the keyboard obscures it from view. Usually the textboxes slide into view. i don't know why that is not happening here.
please help!
<UserControl x:Class="Controls.EditControl"
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:SampleData="clr-namespace:SampleData"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Height="800" Width="480"
d:DataContext="{d:DesignData ../SampleData/sampleEditPopup.xaml}">
<Grid x:Name="LayoutRoot" Background="#FF000000" Opacity="0.995">
<StackPanel Margin="0,20,0,0" Orientation="Horizontal" VerticalAlignment="Top">
<TextBlock TextWrapping="Wrap" Text="Name" HorizontalAlignment="Left" Height="26" Margin="15,10,0,10" Width="110" TextAlignment="Right"/>
<TextBox x:Name="tb_name"
TextWrapping="Wrap"
Width="340" Height="75"
Margin="10,13,15,12"
InputScope="Text" MaxLength="1000"
Text="{Binding Title, Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,150,0,0" VerticalAlignment="Top">
<TextBlock TextWrapping="Wrap" Text="Description" HorizontalAlignment="Left" Height="26" Margin="15,40,0,10" Width="110" VerticalAlignment="Top" TextAlignment="Right"/>
<TextBox x:Name="tb_description"
TextWrapping="Wrap"
Width="340" Height="254"
Margin="10,13,15,12"
InputScope="Text" MaxLength="1000"
Text="{Binding Description, Mode=TwoWay}"/>
</StackPanel>
</Grid>
and here is the code to open it:
EditControl ec = new EditControl();
ec.Title = cm.Title;
ec.Description = cm.Description;
//sets appbar icons for accepting values
setEditIcons();
Popup edit = new Popup() { Child = ec, Tag = this };
edit.Closed += new EventHandler(edit_Closed);
edit.IsOpen = true;
from a UX perspective I'd heavily recommend against having any partial popup/overlay that has more than 1 or 2 buttons. It's just not done in WP7. If you have a form you'd like folks to fill out move it to its own page. Either by letting the user navigate to that page, or by introducing a new transient page (that cannot be navigated back into) prior to the current page.
You'll have more problems than just the on-screen keyboard in with form overlays. You'll need to change the AppBar to have "V" and "X" buttons for any data entry, you'll need to manage focus not slipping back into under the overlay/popup, you'll need to make sure the "back" button closes the popup, etc.
IMO The best approach is a simple and consistent UX.
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.