I have a WPF UserControl with Focusable="True". It's the only focusable control in the window.
Whenever the user presses Tab or Alt (and especially when they Alt+Tab to another application), my UserControl acquires a dotted-line border, aka focus rectangle. The focus rectangle then stays there until the window is closed.
How can I prevent my UserControl from ever displaying this focus rectangle?
Edit
It turns out the focus rectangle wasn't actually being displayed by my UserControl. My Focusable UserControl contained another UserControl that, in turn, contained an ItemsControl, and the ItemsControl is what was showing the focus rectangle.
When I added FocusVisualStyle="{x:Null}" to the ItemsControl, the focus rectangle went away.
If you want to not display the focus rectangle in any case you could set the FocusVisualStyle to null.
<MyControl FocusVisualStyle="{x:Null}" />
Related
I have a DataGridControl,and its cell's DataTemplate overwritten to TextBoxs,by clicking outside the cells but still on the DataGrid, I want the TextBox to lose Keyboard focus so that it can Commit the change, but it seems the DataGrid won't handle the MouseLeftButtonDown event, so I have to manually add a handler to the Grid,and in the handler:
e.Handled = true;
Keyboard.Focus( sender as UIElement );
to make the parent panel "focusable".
By using Snoop, I notice that controls like TextBox, Button are capable of handle MosueLeftButtonDown event, while Panels are not,event if set "Focusable" property to "True". Does anyone know the reason behind this, Thanks.
To simplify the situation: suppose we have a TextBox and a Button on a Grid:
<Grid Background="AliceBlue">
<TextBox Height="25" Margin="50" Text="abcd"/>
<Button Height="25" Margin="50,100,50,0"></Button>
</Grid>
when I click on the TextBox, it gets KeyBoard focus, when I click the blank area of the Grid, I want the TextBox to lose focus, the problem is Grid is not focusable compared with TextBoxes and Buttons.
The documentation for Panel shows that Panel can handle mouse events:
https://msdn.microsoft.com/en-us/library/system.windows.controls.panel_events(v=vs.110).aspx
I have Scrollviewer which contains a frame with a WindowsFormsHost. The WindowsFormsHost contains a DataGridView (please don't ask why I'm not doing this with a WPF DataGrid Control).
Because the DataGridView causes display errors while scrolling with the scrollviewer I disabled the scrollviewer and enabled the scrolling on my DataGridView.
<Grid x:Name="LayoutRoot">
<WindowsFormsHost HorizontalAlignment="Stretch" Name="_windowsFormsHostGrid" VerticalAlignment="Stretch">
<Win.Grid:DataGridView x:Name="_buchungGrid" ScrollBars="Both" BorderStyle="None" BackgroundColor="#F7F8FA" CellFormatting="_gridBuchungen_CellFormatting" SelectionChanged="GridSelectionChanged" DoubleClick="_buchungInovaGrid_DoubleClick" AutoSize="True" AutoColumnWidthMode="Window" ZebraColor="LightGray" Anchor="Left" Dock="Fill" />
</WindowsFormsHost>
</Grid>
This seems to work. As long I don't resize the Window. When I resize the window (and this will cause all child elements to resize including scrollviewer, frame and WindowsFormsHost), the scrollbars of my DataGridView disappears and I'm not longer able to scroll my grid. I can resize to the old size of the window, but the scrollbars are still hidden.
Any idea why this happens and how to fix it? I'm also not sure why they disappear because I'm resizing just one pixel and this occurs.
There seems not to be a solution for this problem. I ended up by using a WPF DataGrid and extend it by the functionality I need.
WinForms will be painted all the time at the top of all other elements. The only solution seems to wrap a windows around it to get scrolling fixed but this would ugly as hell.
Given a Forms.DataGridView (dgv) inside a Forms.UserControl (myUserControl) inside a WindowsFormsHost, I discovered that the DGV was given larger dimensions than the UC, so the scrollbars were not visible. (If UC is instead in a WinForm, scrollbars appear as expected; there seems to be an issue with resize logic inside WFHost.)
I was able to fix this in my UC's SizeChanged handler:
// VB code:
Public Class MyUserControl
...
Private Sub MyUserControl_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
' dgv.Size is much larger than it should be; not sure why.
' my dgv has controls above it, but extends all the way to bottom and right;
' if yours does not, then subtract more as needed.
dgv.Size = New Size(Me.Size.Width - dgv.Left, Me.Size.Height - dgv.Top)
End Sub
...
End Class
The result is that the WinForms drawing stays within the area it is supposed to; the scrolling is done in WinForms. (NOT using a WPF scrollviewer.)
XAML for WPF:
...
xmlns:mywf="clr-namespace:MyWinFormAssembly;assembly=MyWinFormAssembly"
...
<WindowsFormsHost>
<mywf:MyUserControl />
</WindowsFormsHost>
I'm still new to WPF, and I'm trying to do something that's beyond my knowledge at the moment.
I have a listbox databinded to the source collection, and a label. I'd like to bind the label's Content value to the listbox's item over which is mouse hovered.
Say I have DataTemplate binded to the class MenuItem:
<DataTemplate DataType="{x:Type local:MenuItem}" x:Key="MenuListTemplate">
Which has member Text. I want my Label to display Text from element which is mouse overed in list. I have the IsMouseOver trigger for my textbox, but have no idea how to bind Label.Content to it.
Any tips?
I don't think that binding can achieve your goal with ease. I think it's easier to do with routed events.
Subscribe to the MouseMove event at the ListBox level. Check if the source of the event is a ListBoxItem, and if it is use this item to update the label.
Here is the sample pseudo XAML code
<Window>
<Grid Rows="2">
<Listbox Row="0"/>
<Button Row="1"/>
</Grid>
</Window>
Grid doesn't work here, just for example
Listbox is databinded and can have multiple items
Button is placed under the ListBox - immediately under, not on bottom of window
Listbox can grow, moving Button down until Button is on bottom of window. Then Listbox gets vertical scrollbar and scrolls its items, with Button remaining on bottom.
I can't remember seeing such layout, and think it can't be done without binding to ActualHeight/using some converter code, which i'm really bad.
Thanks in advance.
Use a StackPanel instead of your Grid.
I am attempting to add a WPF usercontrol to an existing WinForms project and get the WPF UserControl to dock and fill the entire space.
There's a current framework that loads WinForms UserControls into a parent form (into a panel) in response to button clicks. This is where I'm trying to hook in - The WinForms UserControl that's currently getting loaded will have the ElementHost.
Hierarchy:
Form1.cs - contains a panel that gets WinForms UserControls dynamically loaded
WinForms UserControl - contains the ElementHost
WPF UserControl
The ElementHost has Dock set to Fill and its Child property set to ucReport, which is a WPF UserControl, which has the following markup (only top level design included):
<UserControl x:Class="MyClassName"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TabControl HorizontalAlignment="Left" Name="tabControl1">
<TabItem Header="Header1">
...The interesting stuff goes here
</TabItem>
</TabControl>
</UserControl>
The content of the UserControl does expand vertically when I resize the form, but horizontally, the content only expands large enough to accomodate its content.
When I view the WinForms UserControl (the one that has the ElementHost) in the designer, the problem is apparent. The WPF content that's specified is getting rendered and it's filled top to bottom, but not left to right.
I'm of the mind that it's something in the XAML that has to be set (perhaps on the UserControl declaration?) to get it to fill it's parent container, which is the ElementHost - I just can't find the property.
Would someone enlighten me?
Change HorizontalAlignment to Stretch or get rid of it entirely.