cant add click event to button in wpf - wpf

So I am following a tutorial and I have to add a click event to a button, like I've done many time before. For some reason it give me the following error: Ensure Event Failed.
I did see some questions on the internet that I have to declare a x:Class, but I have done that. It may be incorrect, but I don't know. Below is my XAML code.
<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:img_tutorial"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ShowGridLines="true" x:Name="theGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image HorizontalAlignment="Left" Height="211" VerticalAlignment="Top" Width="387" Source="acces.png" Stretch="UniformToFill"/>
<StackPanel>
<TextBox x:Name="txtImage"></TextBox>
<Button Click="">Add Image</Button>
</StackPanel>
</Grid>
</Window>

I figured it out! The mistake wasn't in my XAML code but in my Visual Basic. For some reason I cannot explain it set the Class as MainWindow3. I changed it to MainWindow and that allowed me to add my event.

Related

Add Right click context menu [duplicate]

This question already has answers here:
how to add context menu in hosted usercontrol?
(1 answer)
How to make right click Button Context menu in WPF?
(2 answers)
Correct binding for ContextMenu in custom control
(3 answers)
Closed 11 months ago.
i'm trying to add to my custom Usercontrol (wpf vb.net ) that have a Grid as layout a dropdown menu (delete copy cut) on right click but every attempt i can't find a solution anyone can give me some advice ? I tried to create a treeview but its not what im trying to achieve, because it works only if i right click on it. but i would like to right click everywhere on the usercontrol and open a menu on mouse position is it possible ?
<UserControl x:Class="UCElement"
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:FlowChartBuilder"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" >
<Grid x:Name="UCElementGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>
</UserControl>
The solution is to explicitly add a ContextMenu control:
<UserControl x:Class="UCElement"
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:FlowChartBuilder"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" >
<!-- ADD THIS -->
<UserControl.ContextMenu>
<ContextMenu Name="ContextMenu">
<MenuItem Header="Delete" Click="DeleteItem" ></MenuItem>
<Separator/>
</ContextMenu>
</UserControl.ContextMenu>
<!-- END -->
<Grid x:Name="UCElementGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>
</UserControl>

Window Auto sizing when adding grid row via button [duplicate]

I have a dialog containing 2 TextBlocks, a Progress Bar and a cancel Button.
Here is the XAML:
<Window x:Class="WpfApplication4.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:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="txtFirst" Grid.Row="0" Margin="5" TextWrapping="Wrap">This is a really really really really long string that wraps</TextBlock>
<TextBlock x:Name="txtSecond" Grid.Row="1" Margin="5" Text="A Shorter string" TextWrapping="Wrap" MaxWidth="200"/>
<ProgressBar x:Name="prgProgress" Grid.Row="2" Margin="5" Height="20" />
<Button x:Name="btnCancel" Grid.Row="3" Margin="5" Height="25" Width="50"/>
</Grid>
</Window>
I would like the Window not to have a fixed height but auto adjust its height based on the size of its children and no more, but can’t see a way to do this. At the moment when I don’t assign anything to the Window’s height, it seems to adopt a height that is much bigger that the content.
Not sure why, or where it gets height value from? If I set Windows Height = “Auto” I get the same thing. All the heights for the RowDefinitions are set to “Auto”, which I take to mean ‘set row height to be row child height’.
You need to use SizeToContent property, check the msdn link.
Example:
<Window 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"
…
SizeToContent="WidthAndHeight">

Button events sometimes triggered, sometimes not

I have a window that looks like this:
<vk:VirtualKeyboard x:Class="Hurst.VirtualKeyboard.QwertyVirtualKeyboard"
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"
xmlns:vk="clr-namespace:Hurst.VirtualKeyboard"
xmlns:vm="clr-namespace:Hurst.VirtualKeyboard.ViewModels"
DataContext="{DynamicResource keyboardViewModel}"
Height="400" Width="1280" d:DesignHeight="300" d:DesignWidth="710"
x:Name="VK"
>
<vk:VirtualKeyboard.Resources>
<ObjectDataProvider x:Key="keyboardViewModel" ObjectType="{x:Type vm:KeyboardViewModel}" />
</vk:VirtualKeyboard.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="43"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Foreground="White" Margin="0 3 20 0" Background="#006AB6" BorderThickness="0" Command="{Binding CloseKeyboardCommand}" FontSize="25" Content="Close" VerticalContentAlignment="Center"/>
<Border Grid.Row="1" BorderBrush="#006AB6" BorderThickness="5">
some content here
</Border>
</Grid>
QwertyVirtualKeyboard is a class that extends the VirtualKeyboard class, which extends the Window class.
The problem I am facing is this: When I click on the Close button, sometimes the Close command is triggered and sometimes not. The same things happens with hover - sometimes hover is triggered, and sometimes not. I've noticed that when the mouse is over the bottom side of the button, hover and click work as they should, but when I move the mouse to the upper part of the button they dont. What am I missing here?

WPF Toolbar buttons don't seem to respond

I have the following code:
<ToolBarTray Margin="0,21,0,0" Width="Auto" Height="38" VerticalAlignment="Top">
<ToolBar Height="38">
<Button Style="{StaticResource ResourceKey=btnStyle}" Command="Cut" IsEnabled="True">
<Image Source="images/teren.png" ToolTip="Test" />
</Button>
</Toolbar>
</ToolBarTray>
The style in question only changes the height and width at the moment. Every element is drawn accordingly, but the button seems to be non functional in the sense that for all purposes it looks like an image rather than anything else. The tooltip won't display, it has no hover animation and it can't really be pressed.
I'm new to WPF so I'm guessing I missed something big.
The problem isn't in the image. If I remove that line, it still doesn't act like a button at all.
The reason the button is greyed out is because you have told it to use the built-in cut command. This means that the Button will be automatically disabled when there is nothing to cut and enabled when something that can be cut is selected (such as text).
To verify this you can do two things; either remove the cut command and see that the button is now enabled:
<Window x:Class="WpfApplication1.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>
<ToolBarTray Margin="0,21,0,0" Width="Auto" Height="38" VerticalAlignment="Top">
<ToolBar Height="38">
<Button IsEnabled="True">
Click
</Button>
</ToolBar>
</ToolBarTray>
</Grid>
</Window>
Or add a rich text box control and see that the button is enabled when you select some text:
<Window x:Class="WpfApplication1.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>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ToolBarTray Width="Auto" VerticalAlignment="Top">
<ToolBar Height="38" >
<Button IsEnabled="True" Command="Cut">
Click
</Button>
</ToolBar>
</ToolBarTray>
<RichTextBox Grid.Row="1"/>
</Grid>
</Window>

Silverlight DockPanel exception in runtime

I have a DockPanel in a UserControl, and in the designer looks everything fine, but I get an exception in runtime from the InitializeComponent():
The type 'DockPanel' was not found because 'http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit' is an unknown namespace.
Any idea?
<UserControl x:Class="Controls.PropertiesControl"
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:my="clr-namespace:Controls"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="800"
>
<Grid x:Name="LayoutRoot" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<toolkit:DockPanel Background="Gray" Grid.Row="0" Grid.Column="0" />
</Grid>
</Grid>
</UserControl>
Do you reference the correct System.Windows.Controls.Toolkit.dll assembly in your project? If you're using Silverlight 4, also make sure it's the right version 4 (not 3).

Resources