WPF - databinding label on listbox ismouseover - wpf

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.

Related

How to show busy indicator for a WPF ItemsControl?

I have an ItemsControl where I'm binding the ItemsSource property to my ViewModel. It takes a few moments for the DataTemplate to render. I would like to display a "busy indicator" while the DataTemplate renders.
<ItemsControl ItemsSource="{Binding Request.RequestDiscussions}">
Can this be done with DataTriggers or can it be done by the use of the ItemsControl's events? Basically, I need an event when the binding begins and when the rendering has completed.
I wrap the item I want to have a busy indicator in a grid and add the indicator as a sibling that is centered horizontally and vertically. This lets them overlap nicely. Then I bind the visibility of the indicator to an IsBusy property on my viewmodel letting the bindings take care of everything.

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.

Prevent users from changing DataGrid's SelectedItem

I have a WPF 4.0 application and I am using the WPF DataGrid. What I want is to use navigation buttons on my view to change the SelectedItem the DataGrid, rather than letting the user change the SelectedItem by clicking on the DataGrid. I am using an ICollectionView in my ViewModel as the ItemsSource for my DataGrid. Here is what I have so far:
NextCommand (ViewModel):
DefaultView.MoveCurrentToNext(); // DefaultView is an ICollectionView
SelectedItem = DefaultView.CurrentItem as MyProperty;
DataGrid (View):
<DataGrid ItemsSource="{Binding Path=DefaultView}"
SelectedItem="{Binding Path=SelectedItem}"
IsSynchronizedWithCurrentItem="True">
...
</DataGrid>
The navigation buttons work great... however, I do not want to allow the user to click on the DataGrid to change the SelectedItem. Any ideas on how to accomplish this? I've played around with the DataGrid_SelectionChanged event, but the problem is that the binding on the SelectedItem updates the ViewModel before this event even fires. I would prefer that the SelectedItem does not get changed twice (once when the user clicks, and twice when it is set back to the original). I am ok with using the Code behind if needed...
Apparently you have to disable it through a custom DataGrid Template:
http://www.wpfsharp.com/2011/05/how-to-disable-row-selection-in-a-wpf-datagrid/
Out of curiosity, why do you want to do this? I mean why do you want to forbid a user from selecting a row by clicking it, but yet allow the user to select a row indirectly by using some navigation buttons?

How to set focus on element on ItemsControl?

I've ItemsControl that its ItemsSource property bind to some dictionary from code behind. The ItemTemplate is consist of only one button. So, for each item in dictionary it creates button.
My question is how can I set focus to one of these buttons (dynamically)?
Should I use ItemContainerGenerator.ContainerFromItem ?
Any other idea?
Thanks in advance!
Yes, use can ItemContainerGenerator.ContainerFromItem to get a container for your data item, then you will need to find you button inside this container and call Focus() on the button.
OR you can use an attached property to bind IsFocused to a property on your data item. See Set focus on textbox in WPF from view model (C#)

ListBox instead of ItemsPresenter in WPF Custom Control?

I'm writing a generic control template for my WPF Custom Control.
But with ItemsPresenter I only got raw list of Data..
Compared to the ListBox, the ListBox has all features I need.
Is it wrong to use a ListBox instead of ItemsPresenter ?
What I'm after is that
if I write a generic Template that uses a ListBox and in code behind I register some ListBox specific events and somebody overrides my generic Template with his own ControlTemplate witn an ItemsControl inside that does not possess that event, it will raise an Exception. In case of ItemsPresenter, everyone could use what he wants to.
Thanks.
I think you could add some test to see if the ItemsControl in the template is a ListBox or not. For example:
var itemsControl = this.Template.FindName("PART_Items", this);
if(itemsControl is ListBox)
{
// wire additional event handler here
}

Resources