Testing the scrollbar visibility of a ListBox in code-behind - wpf

How to check whether the vertical scrollbar of the listbox is visible in code-behind?
I have a listbox with x:Name="listOfItems" and its underlying ScrollViewer's VerticalScrollbarVisibility is set to auto.
When the ItemsSource property of the ListBox is set, I want to check whether the verticalScrollbar is visible, but I don't know which property to check or how to dive into the scrollviewer element of the listbox.
Any suggestions

You can find Listbox' ScrollViewer as described here: WPF - Animate ListBox.ScrollViewer.HorizontalOffset?
Then you can use ComputedVerticalScrollBarVisibility property to check if the scrollbar is visible:
ScrollViewer sv = FindVisualChild<ScrollViewer>(listOfItems);
Visibility scrollbarVisibility = sv.ComputedVerticalScrollBarVisibility;

Related

How to achieve ItemSelection and IsMouseOver effect in ItemsControl WPF?

I am using ItemsControl in my application, applied ItemTemplate and ItemsPanelTemplate to it with a ScrollViewer style. All is fine. Binding is working pretty fine but here the issue: I want to select an item in ItemsControl but I am not able to do so as we do in ListBox using IsSelected property in triggers.
How can I achieve item selection and IsMouseOver in ItemsControl similar to ListBox, is there any way, as I did not find any resource on net useful enough so thought to ask for some help here.

How to make WPF DataGrid VerticalScrollBar Always Visible?

Do you knows how to make the WPF DataGrid VerticalScrollBar always visible, even if there is no data to display without putting DataGrid object instid a ScrollViewer?
You should be able to set the ScrollViewer.VerticalScrollBarVisibility attached property like so:
<DataGrid ScrollViewer.VerticalScrollBarVisibility="Visible" ...

WPF - databinding label on listbox ismouseover

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.

WPF DataGrid Hide selected row?

Is there a way to "hide" the SelectedItem of the DataGrid ?
dataGrid.SelectedItem.Visibility = Visibility.Hidden
Setting the Visibility of "SelectedItem", as you have, would not change the grid - it would be changing the "SelectedItem" property value of the bound object for that SelectedItem, which is not going to affect the grid at all.
In order to change the grid row's visibility, you'll need to change it directly. You can retemplate the DataGrid to change how the selected row is displayed.
For details, see DataGrid Styles and Templates.

Remove focus rectangle on a UserControl

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}" />

Resources