Routed event does not work - wpf

I am new to WPF and have a problem.
I want to handle a button event in its parent that is a grid element
but it does not show ButtonBase.click for me.
Can anyone tell me why?
Did I miss something?
this is the code:
<Window x:Class="CH01.CustomAttached.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CH01.CustomAttached"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" Width="75" Click="Button_Click"/>
</Grid>
</Window>

Related

Loading page as frame content in WPF, but dynamic resources in loaded page won't update dynamically?

So, I've gone and did what Edd asked and rewrote this. The problem is a textblock with a dynamic resource as its content, updates properly when the textblock is on the mainwindow. But when I try to load page1 into a frame on the mainwindow, it loads but does not update the dynamic resource. I'm a total noob so not sure what I'm even looking for to find what's causing the issue, let alone fix it. Any help appreciated!
This is the application.xaml:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<sys:String x:Key="TestString">This string is for testing.</sys:String>
</Application.Resources>
</Application>
This is the MainWindow.xaml:
<Window x:Class="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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
<TextBlock HorizontalAlignment="Left" Margin="125,299,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
<Button Content="Call Page" HorizontalAlignment="Left" Margin="165,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="Change Text" HorizontalAlignment="Left" Margin="490,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
</Window>
This is the Page1.xaml:
<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"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="125,140,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
</Grid>
</Page>
And this is the MainWindow.xaml.vb (Code-Behind):
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
MainFrame.Content = New Page1
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Resources("TestString") = "Test Completed."
End Sub
End Class
When I run the program and press both buttons, this is the result:
Image of the result
The string updates for the Textblock on the MainWindow, but not for the one on Page1 called into the frame.

Binding Window1 Icon to MainWindow Icon

I tried following code but it doesnt work. Any suggestions?
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1" x:Name="Window1" Title="Window1"
Height="300" Width="300"
Icon="{Binding ElementName=Application.Current.MainWindow, Path=Icon}" >
<Grid>
</Grid>
This should work:
<Window ...
Icon="{Binding Path=MainWindow.Icon, Source={x:Static Application.Current}}">
<Grid>
</Grid>
</Window>

WPF Control size is not the same at runtime

I am creating a new WPF application with visual studio 2015 Community.
I put some buttons in visual studio XAML editor.
I can see in editor I have big buttons, with a big font But when i run project, i see standard windows buttons.
Do you have any idea ?
Thanks
* Edit *
Here is my XAML Code:
<Window x:Class="MyProject.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:MyProject"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Loaded="Grid_Loaded">
<MediaElement LoadedBehavior="Manual" Name="video" Stretch="None" />
<Button x:Name="button1" Content="Button1" HorizontalAlignment="Left" Margin="432,10,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>

Image is not stretched to the whole client area

Visual Studio 2012, WPF C# Windows Application. XAML:
<Window x:Class="Edge.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Background="Blue">
<Image Source="Images/House.png"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Fill" Margin="3"
/>
</Grid>
</Window>
Image is not stretched to the whole window, as I want, it is shown only in 1/4 window area. I expect that Stretch="Fill" must stretch the image to the whole window. What is wrong?
The problem is your outer Grid. A Grid will re-size to fit the size of its children, but the child in this case (the Image) is trying to stretch to fill the size of its parent so I'm going to guess that what you are seeing is the image being drawn actual size.
Try using a DockPanel instead which fills all available parent space children.
<Window x:Class="Edge.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel Background="Blue">
<Image Source="Images/House.png"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Fill" Margin="3"/>
</DockPanel>
</Window>

Visual Studio 2012 XAML Designer - Cannot add more than one item

I'm new to VS 2012, and I have this issue every time I use the XAML Designer.
Every time I add an item (e.g. a RadioButton, and Image, a Label) to my window, it deletes the previous one.
As a result, I can have only one item in my window, I know it is absurd, what am I missing?
Here is the xaml of the window
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="400" Width="600">
<Toolkit:KinectSensorChooserUI x:Name="SensorChooserUI" VerticalAlignment="Center" Height="40" Margin="277,2,275,328"/>
</Window>
Here is the xaml of the window after dragging an image on it (from the Toolbox)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="400" Width="600">
<Image HorizontalAlignment="Left" Height="86" Margin="77,188,0,0" VerticalAlignment="Top" Width="111"/>
</Window>
As made clear by #Hans, I was trying to add multiple content items into a Window, in the XAML designer. This is just not possible so i had to:
1) Add a Grid to the Window.
2) Add any item to the grid.
It works, here is a sample code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfViewers="clr-namespace:Microsoft.Samples.Kinect.WpfViewers;assembly=Microsoft.Samples.Kinect.WpfViewers" xmlns:Toolkit="clr-namespace:Microsoft.Kinect.Toolkit;assembly=Microsoft.Kinect.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="KinectSetupDev.MainWindow"
Title="MainWindow" Height="768" Width="1024" Loaded="Window_Loaded_1">
<Grid HorizontalAlignment="Left" Height="736" VerticalAlignment="Top" Width="1012" Margin="2,2,0,0">
<Image x:Name="Image01" HorizontalAlignment="Left" Height="240" Margin="136,27,0,0" VerticalAlignment="Top" Width="320"/>
<TextBlock x:Name="tbMessages" HorizontalAlignment="Left" Height="60" Margin="10,606,-664,-426" TextWrapping="Wrap" VerticalAlignment="Top" Width="974"/>
<WpfViewers:KinectColorViewer HorizontalAlignment="Left" Height="240" Margin="666,0,-666,0" VerticalAlignment="Top" Width="320"/>
</Grid>
</Window>

Resources