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?
Related
I need to display a floating image in WPF and it needs to support the following operations
The user should be able to click the image (like a toggle button).
The user should be able to drag the image anywhere on the screen.
I am using PreviewMouseDown event on the window to handle the drag and created InputBindings for Left Click on the Image control to handle the click event on the Image.
The Click command handler is not getting called.
Below is the XAML
<Window x:Class="Views.ImageControlTest"
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"
mc:Ignorable="d"
Height="130" Width="224"
WindowStyle="None"
AllowsTransparency="True"
ResizeMode="NoResize"
PreviewMouseDown="Window_PreviewMouseDown">
<Window.Background>
<SolidColorBrush Opacity="0.0" Color="White"/>
</Window.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="108"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Cursor="Hand" Source="{Binding ImagePath}" Height="108" Width="108">
<Image.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ClickCommand, Mode=OneTime}" />
</Image.InputBindings>
</Image>
<TextBlock Grid.Row="1" Background="LightGray" Text={Binding State} FontSize="14" Height="20" Width="224" FontFamily="Segoe UI"></TextBlock>
</Grid>
</Window>
InputBinding is a highler level API that relies on core WPF mouse and keyboard events. Thus, if you handle the bubbling PreviewMouseDown on the Window, its LeftMouseButtonDown counterpart will not get raised by the original source (the Image) and there will be no event to trigger the gesture. Consider another approach that will not require handiling the preview event.
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.
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">
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>
I have a user control and within it I have two controls: 1) a search criteria control (custom user control) and 2) a Telerik RadGridView.
Apart from that I have a window that is the shell for my application and in it exists three regions. When this window opens I click a button and the user control mentioned above gets inserted into one of the regions. Now that the user control is loaded I edit my search criteria and click the button to populate my grid with around 1,000 records. The problem is that the grid's scroll bar doesn't become enabled because the grid control maximizes to fit the size of all the data which runs past the screen.
The only way this works properly is to set the Height property of the grid to an absolute value but then the grid doesn't size with the window.
I've tried everything that I can think of: When in the grid I set the row def size to 200*, I put the grid in a wrap panel, I put it in a dockpanel, set the VerticalAlignment to stretch, changed the dock panel to stack panel.
Shell:
<Window x:Class="WTS.CGCApplicationInterface.CGCApplicationShellView"
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:cal="http://www.codeplex.com/CompositeWPF"
xmlns:inf="clr-namespace:WTS.CGCApplicationInterface.Infrastructure;assembly=WTSCGCApplicationInterface.Infrastructure"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Width="750" Height="480"
Title="CGC Interface Window">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="200*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ItemsControl cal:RegionManager.RegionName="MenuRegion" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" />
<ItemsControl cal:RegionManager.RegionName="SwitchboardRegion" Grid.Column="0" Grid.Row="1" />
<ItemsControl cal:RegionManager.RegionName="MainRegion" Grid.Column="1" Grid.Row="1" />
</Grid>
User Control:
<UserControl x:Class="WTS.CGCApplicationInterfaceMVVM.Views.Oncor.OncorTimesheetCGC"
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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:wtsControls="clr-namespace:WTS.CGCApplicationInterface.UserControls;assembly=WTSCGCApplicationInterface.Infrastructure"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600" Background="Red">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="200*" />
</Grid.RowDefinitions>
<wtsControls:CGCQueryUserControl Grid.Row="0"/>
<telerik:RadGridView ItemsSource="{Binding Path=Timesheets}" AutoGenerateColumns="False" Grid.Row="1"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<telerik:RadGridView.Columns>
<telerik:GridViewToggleRowDetailsColumn></telerik:GridViewToggleRowDetailsColumn>
<telerik:GridViewDataColumn Header="MyField1" UniqueName="AssociatedObject.MyField1" IsVisible="False" />
<telerik:GridViewDataColumn Header="MyField2" UniqueName="AssociatedObject.MyField2" IsVisible="False" />
I wish I could get points for answering my own question. I do realize that this could be taken advantage of. That said, in my shell, I changed the ItemsControl region to a ContentControl and it works perfectly.