Iam using MVVMlight for windows 8.1 app. I want to navigate to a new view when list Item is clicked and pass the clicked item as parameter.
I have defined An attached property like:
public static class ItemClickCommand
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand),
typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged));
public static void SetCommand(DependencyObject d, ICommand value)
{
d.SetValue(CommandProperty, value);
}
public static ICommand GetCommand(DependencyObject d)
{
return (ICommand)d.GetValue(CommandProperty);
}
private static void OnCommandPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var control = d as ListViewBase;
if (control != null)
control.ItemClick += OnItemClick;
}
private static void OnItemClick(object sender, ItemClickEventArgs e)
{
var control = sender as ListViewBase;
var command = GetCommand(control);
if (command != null && command.CanExecute(e.ClickedItem))
command.Execute(e.ClickedItem);
}
}
The xaml in the view looks like:
<GridView
ItemsSource="{Binding Source={StaticResource ItemsSource}}"
ItemTemplate="{StaticResource itemsTemplate}"
SelectionMode="None"
helpers:ItemClickCommand.Command="{Binding ItemClicked}"
>
</GridView>
And the view model:
private RelayCommand<Item> _ItemClicked;
public RelayCommand<Item> ItemClicked
{
get
{
if (_ItemClicked == null)
{
_ItemClicked = new RelayCommand<Item>(
(item) =>
{
_navigationService.Navigate(typeof(ItemsPage));
});
}
return _ItemClicked;
}
}
Nothing happens when I click the Grid Item.
I resorted to this tutorial by: Laurent Bugnion
http://msdn.microsoft.com/en-us/magazine/dn237302.aspx
Related
I have DataGrid, which has got 2 modes, ListView and alleryView. When we select an item in GalleryView and switch to ListView the same item gets selected in list view too. But scroll does not scrolls down to the selected item automatically. How to make it work automatically?
XAML File
<DataGrid ItemsSource="{Binding ListItems}" RowStyle="{StaticResource DataGridRowStyle}"
AutoGenerateColumns="False" RowHeight="60" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserResizeRows="False" AlternationCount="2"
HorizontalGridLinesBrush="LightSteelBlue" VerticalGridLinesBrush="LightSteelBlue"
SelectionMode="Single" SelectedItem="{Binding SelectedSearchItem}"
IsReadOnly="True" KeyboardNavigation.TabNavigation="Once" behaviors:DataGridBehavior.Autoscroll="{Binding Autoscroll}" >
CS File
private void SetSelectedItemOnViewChange()
{
if (SelectedViewMode.ModeName == ViewModes[1].ModeName)
GallerySearchResults.SelectedSearchItem = GallerySearchResults.GalleryItems.FirstOrDefault((x => x.IndexNo == SelectedRecordIndex));
else if (SelectedViewMode.ModeName == ViewModes[0].ModeName)
{
ListSearchResults.SelectedSearchItem = ListSearchResults.ListItems.FirstOrDefault((x => x.IndexNum == SelectedRecordIndex));
if (SelectedRecordIndex > 10)
Autoscroll = true;
}
}
Behavior file:
public static class DataGridBehavior
{
public static readonly DependencyProperty AutoscrollProperty = DependencyProperty.RegisterAttached(
"Autoscroll", typeof(bool), typeof(DataGridBehavior), new PropertyMetadata(default(bool), AutoscrollChangedCallback));
private static readonly Dictionary<DataGrid, NotifyCollectionChangedEventHandler> handlersDict = new Dictionary<DataGrid, NotifyCollectionChangedEventHandler>();
private static void AutoscrollChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var dataGrid = dependencyObject as DataGrid;
if (dataGrid == null)
{
throw new InvalidOperationException("Dependency object is not DataGrid.");
}
if ((bool)args.NewValue)
{
Subscribe(dataGrid);
dataGrid.Unloaded += DataGridOnUnloaded;
dataGrid.Loaded += DataGridOnLoaded;
}
else
{
Unsubscribe(dataGrid);
dataGrid.Unloaded -= DataGridOnUnloaded;
dataGrid.Loaded -= DataGridOnLoaded;
}
}
private static void Subscribe(DataGrid dataGrid)
{
var handler = new NotifyCollectionChangedEventHandler((sender, eventArgs) => ScrollToEnd(dataGrid));
handlersDict.Add(dataGrid, handler);
((INotifyCollectionChanged)dataGrid.Items).CollectionChanged += handler;
ScrollToEnd(dataGrid);
}
private static void Unsubscribe(DataGrid dataGrid)
{
NotifyCollectionChangedEventHandler handler;
handlersDict.TryGetValue(dataGrid, out handler);
if (handler == null)
{
return;
}
((INotifyCollectionChanged)dataGrid.Items).CollectionChanged -= handler;
handlersDict.Remove(dataGrid);
}
private static void DataGridOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
var dataGrid = (DataGrid)sender;
if (GetAutoscroll(dataGrid))
{
Subscribe(dataGrid);
}
}
private static void DataGridOnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
var dataGrid = (DataGrid)sender;
if (GetAutoscroll(dataGrid))
{
Unsubscribe(dataGrid);
}
}
private static void ScrollToEnd(DataGrid datagrid)
{
if (datagrid.Items.Count == 0)
{
return;
}
datagrid.ScrollIntoView(datagrid.Items[datagrid.Items.Count - 1]);
}
public static void SetAutoscroll(DependencyObject element, bool value)
{
element.SetValue(AutoscrollProperty, value);
}
public static bool GetAutoscroll(DependencyObject element)
{
return (bool)element.GetValue(AutoscrollProperty);
}
}
xaml.cs file
public partial class ItemGridControl : UserControl
{
public ItemGridControl()
{
InitializeComponent();
}
}
These are the I am working with. But the changes are not getting reflected. Item is getting selected on switching but the scroll bar is not going to bottom. I want it to go to bottom of the page when item number ie., SelectedRecordIndex is greater than 10
To scroll DataGrid to particular row (or column) you can use DataGrid.ScrollIntoView, which has two overloads:
ScrollIntoView(Object) - scrolls to specified item in datagrid view (i.e. row)
ScrollIntoView(Object, DataGridColumn) - scrolls to specified item and column
In your case you rather need first overload. In order oto scroll to last row you could use such code:
myDataGrid.ScrollIntoView(myDataGrid.Items[myDataGrid.Items.Count - 1]);
If you want to do it in the ViewModel you can use a Behaviour.
You can check it in: Scroll WPF ListBox to the SelectedItem set in code in a view model
There explains how to do it with a ListBox, it's the same with a DataGrid.
I created a UserControl1 that wraps a DataGrid (this is simplified for test purposes, the real scenario involves a third-party control but the issue is the same). The UserControl1 is used in the MainWindow of the test app like so:
<test:UserControl1 ItemsSource="{Binding People,Mode=OneWay,ElementName=Self}"
SelectedItems="{Binding SelectedPeople, Mode=TwoWay, ElementName=Self}"/>
Everything works as expected except that when a row is selected in the DataGrid, the SelectedPeople property is always set to null.
The row selection flow is roughly: UserControl1.DataGrid -> UserControl1.DataGrid_OnSelectionChanged -> UserControl1.SelectedItems -> MainWindow.SelectedPeople
Debugging shows the IList with the selected item from the DataGrid is being passed to the SetValue call of the SelectedItems dependency property. But when the SelectedPeople setter is subsequently called (as part of the binding process) the value passed to it is always null.
Here's the relevant UserControl1 XAML:
<Grid>
<DataGrid x:Name="dataGrid" SelectionChanged="DataGrid_OnSelectionChanged" />
</Grid>
In the code-behind of UserControl1 are the following definitions for the SelectedItems dependency properties and the DataGrid SelectionChanged handler:
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(UserControl1), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));
public IList SelectedItems
{
get { return (IList)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
private bool _isUpdatingSelectedItems;
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctrl = d as UserControl1;
if ((ctrl != null) && !ctrl._isUpdatingSelectedItems)
{
ctrl._isUpdatingSelectedItems = true;
try
{
ctrl.dataGrid.SelectedItems.Clear();
var selectedItems = e.NewValue as IList;
if (selectedItems != null)
{
var validSelectedItems = selectedItems.Cast<object>().Where(item => ctrl.ItemsSource.Contains(item) && !ctrl.dataGrid.SelectedItems.Contains(item)).ToList();
validSelectedItems.ForEach(item => ctrl.dataGrid.SelectedItems.Add(item));
}
}
finally
{
ctrl._isUpdatingSelectedItems = false;
}
}
}
private void DataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_isUpdatingSelectedItems && sender is DataGrid)
{
_isUpdatingSelectedItems = true;
try
{
var x = dataGrid.SelectedItems;
SelectedItems = new List<object>(x.Cast<object>());
}
finally
{
_isUpdatingSelectedItems = false;
}
}
}
Here is definition of SomePeople from MainWindow code-behind:
private ObservableCollection<Person> _selectedPeople;
public ObservableCollection<Person> SelectedPeople
{
get { return _selectedPeople; }
set { SetProperty(ref _selectedPeople, value); }
}
public class Person
{
public Person(string first, string last)
{
First = first;
Last = last;
}
public string First { get; set; }
public string Last { get; set; }
}
I faced the same problem, i dont know reason, but i resolved it like this:
1) DP
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(object), typeof(UserControl1),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemsChanged));
public object SelectedItems
{
get { return (object) GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
2) Grid event
private void DataGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var SelectedItemsCasted = SelectedItems as IList<object>;
if (SelectedItemsCasted == null)
return;
foreach (object addedItem in e.AddedItems)
{
SelectedItemsCasted.Add(addedItem);
}
foreach (object removedItem in e.RemovedItems)
{
SelectedItemsCasted.Remove(removedItem);
}
}
3) In UC which contain UserControl1
Property:
public IList<object> SelectedPeople { get; set; }
Constructor:
public MainViewModel()
{
SelectedPeople = new List<object>();
}
I know this is a super old post- but after digging through this, and a few other posts which address this issue, I couldn't find a complete working solution. So with the concept from this post I am doing that.
I've also created a GitHub repo with the complete demo project which contains more comments and explanation of the logic than this post. MultiSelectDemo
I was able to create an AttachedProperty (with some AttachedBehavour logic as well to set up the SelectionChanged handler).
MultipleSelectedItemsBehaviour
public class MultipleSelectedItemsBehaviour
{
public static readonly DependencyProperty MultipleSelectedItemsProperty =
DependencyProperty.RegisterAttached("MultipleSelectedItems", typeof(IList), typeof(MultipleSelectedItemsBehaviour),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, MultipleSelectedItemsChangedCallback));
public static IList GetMultipleSelectedItems(DependencyObject d) => (IList)d.GetValue(MultipleSelectedItemsProperty);
public static void SetMultipleSelectedItems(DependencyObject d, IList value) => d.SetValue(MultipleSelectedItemsProperty, value);
public static void MultipleSelectedItemsChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender is DataGrid dataGrid)
{
if (e.NewValue == null)
{
dataGrid.SelectionChanged -= DataGrid_SelectionChanged;
}
else
{
dataGrid.SelectionChanged += DataGrid_SelectionChanged;
}
}
}
private static void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is DataGrid dataGrid)
{
var selectedItems = GetMultipleSelectedItems(dataGrid);
if (selectedItems == null) return;
foreach (var item in e.AddedItems)
{
try
{
selectedItems.Add(item);
}
catch (ArgumentException)
{
}
}
foreach (var item in e.RemovedItems)
{
selectedItems.Remove(item);
}
}
}
}
To use it, one critical thing within the view model, is that the view model collection must be initialized so that the attached property/behaviour sets up the SelectionChanged handler. In this example I've done that in the VM constructor.
public MainWindowViewModel()
{
MySelectedItems = new ObservableCollection<MyItem>();
}
private ObservableCollection<MyItem> _myItems;
public ObservableCollection<MyItem> MyItems
{
get => _myItems;
set => Set(ref _myItems, value);
}
private ObservableCollection<MyItem> _mySelectedItems;
public ObservableCollection<MyItem> MySelectedItems
{
get => _mySelectedItems;
set
{
// Remove existing handler if there is already an assignment made (aka the property is not null).
if (MySelectedItems != null)
{
MySelectedItems.CollectionChanged -= MySelectedItems_CollectionChanged;
}
Set(ref _mySelectedItems, value);
// Assign the collection changed handler if you need to know when items were added/removed from the collection.
if (MySelectedItems != null)
{
MySelectedItems.CollectionChanged += MySelectedItems_CollectionChanged;
}
}
}
private int _selectionCount;
public int SelectionCount
{
get => _selectionCount;
set => Set(ref _selectionCount, value);
}
private void MySelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Do whatever you want once the items are added or removed.
SelectionCount = MySelectedItems != null ? MySelectedItems.Count : 0;
}
And finally to use it in the XAML
<DataGrid Grid.Row="0"
ItemsSource="{Binding MyItems}"
local:MultipleSelectedItemsBehaviour.MultipleSelectedItems="{Binding MySelectedItems}" >
</DataGrid>
I have a strange situation.
I working on an App for WINRT and have some problems with a command binding. This is the part of the xaml:
<control:ItemsHub ItemsSource="{Binding Categories}">
<control:ItemsHub.SectionHeaderTemplate>
<DataTemplate>
<Button Command="{Binding CategoryNavigationCommand}" Margin="5,0,0,10" Content="{Binding Header}"/>
</DataTemplate>
</control:ItemsHub.SectionHeaderTemplate>
</control:ItemsHub>
This is my ViewModel:
public CategorySectionViewModel(IRecipeService recipeService, INavigationService navigationService, RecipeTreeItemDto treeItem)
{
...
CategoryNavigationCommand = new DelegateCommand(NavigateToCategory);
...
}
private string _header;
public string Header
{
get { return _header; }
set { SetProperty(ref _header, value); }
}
public DelegateCommand CategoryNavigationCommand { get; private set; }
private void NavigateToCategory()
{
_navigationService.Navigate("CategoryHub", _recipeTreeItemDto.NodePath);
}
I don't get any binding errors in the output window and also the "Header" gets shown in the Button. But the Command won't get fired wenn i click on it! What i'm doing wrong?
Maybe it is because i created a custom HubControl. With this control i'm able to attach an ItemSource and ItemTemplate for the HubSection-Header and the HubSection-Content. Maybe because of this some bindings getting lost?
Here is my custom hub control:
public class ItemsHub : Hub
{
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public HubSection SpotlightSection
{
get { return (HubSection)GetValue(SpotlightSectionProperty); }
set { SetValue(SpotlightSectionProperty, value); }
}
public DataTemplate SectionHeaderTemplate
{
get { return (DataTemplate)GetValue(SectionHeaderTemplateProperty); }
set { SetValue(SectionHeaderTemplateProperty, value); }
}
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ItemsHub), new PropertyMetadata(null, ItemTemplateChanged));
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IList), typeof(ItemsHub), new PropertyMetadata(null, ItemsSourceChanged));
public static readonly DependencyProperty SpotlightSectionProperty =
DependencyProperty.Register("SpotlightSection", typeof(HubSection), typeof(ItemsHub), new PropertyMetadata(default(HubSection), SpotlightSectionChanged));
public static readonly DependencyProperty SectionHeaderTemplateProperty =
DependencyProperty.Register("SectionHeaderTemplate", typeof(DataTemplate), typeof(ItemsHub), new PropertyMetadata(default(DataTemplate), HeaderTemplateChanged));
private static void SpotlightSectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsHub hub = d as ItemsHub;
if (hub != null)
{
bool hubContainsSpotLight = hub.Sections.Contains(hub.SpotlightSection);
if (hub.SpotlightSection != null && !hubContainsSpotLight)
{
hub.Sections.Add(hub.SpotlightSection);
}
if (hub.SpotlightSection == null && hubContainsSpotLight)
{
hub.Sections.Remove(hub.SpotlightSection);
}
}
}
private static void HeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsHub hub = d as ItemsHub;
if (hub != null)
{
DataTemplate template = e.NewValue as DataTemplate;
if (template != null)
{
// Apply template
foreach (var section in hub.Sections.Except(new List<HubSection> { hub.SpotlightSection }))
{
section.HeaderTemplate = template;
}
}
}
}
private static void ItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsHub hub = d as ItemsHub;
if (hub != null)
{
DataTemplate template = e.NewValue as DataTemplate;
if (template != null)
{
// Apply template
foreach (var section in hub.Sections.Except(new List<HubSection> { hub.SpotlightSection }))
{
section.ContentTemplate = template;
}
}
}
}
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsHub hub = d as ItemsHub;
if (hub != null)
{
IList items = e.NewValue as IList;
if (items != null)
{
var spotLightSection = hub.SpotlightSection;
hub.Sections.Clear();
if (spotLightSection != null)
{
hub.Sections.Add(spotLightSection);
}
foreach (var item in items)
{
HubSection section = new HubSection();
section.DataContext = item;
section.Header = item;
DataTemplate headerTemplate = hub.SectionHeaderTemplate;
section.HeaderTemplate = headerTemplate;
DataTemplate contentTemplate = hub.ItemTemplate;
section.ContentTemplate = contentTemplate;
hub.Sections.Add(section);
}
}
}
}
}
Thanks for your help!
I found the solution! I don't know why but the section header of a hub control is not interactive by default! So i have to set the interactivity to true when i assign the itemsource in my custom ItemHub-Control.
Here is what i changed in the ItemsHub Control:
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsHub hub = d as ItemsHub;
if (hub != null)
{
IList items = e.NewValue as IList;
if (items != null)
{
var spotLightSection = hub.SpotlightSection;
hub.Sections.Clear();
if (spotLightSection != null)
{
hub.Sections.Add(spotLightSection);
}
foreach (var item in items)
{
HubSection section = new HubSection();
DataTemplate headerTemplate = hub.SectionHeaderTemplate;
section.HeaderTemplate = headerTemplate;
DataTemplate contentTemplate = hub.ItemTemplate;
section.ContentTemplate = contentTemplate;
section.DataContext = item;
section.Header = item;
//This line fixed my problem.
section.IsHeaderInteractive = true;
hub.Sections.Add(section);
}
}
}
You can use following binding expression
Command="{Binding CategoryNavigationCommand,RelativeSource={RelativeSource AncestorType=XYX}}"
and XYZ is a control type in which you have placed ItemsHub
I am having a problem binding the TextboxHelper.ButtonCommand (from mahapps.metro) to my view model using caliburn.
At the moment I have this working using a delegate command.
View:
<TextBox Name="TextBoxContent"
mui:TextboxHelper.ButtonContent="s"
mui:TextboxHelper.ButtonCommand="{Binding DelCommand, Mode=OneWay}"
Style="{DynamicResource ButtonCommandMuiTextBox}" />
ViewMode:
ICommand DelCommand
{
get {return new Command();}
}
void Command() { //Handle press here }
However I really would like to use caliburn not the delegate command to acheive this. I have tried using the event triggers on the textbox to no avail, like so...
<TextBox Name="TextBoxContent" mui:TextboxHelper.ButtonContent="s"
Style="{DynamicResource ButtonCommandMuiTextBox}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="mui:TextboxHelper.ButtonCommand">
<i:EventTrigger.Actions>
<cal:ActionMessage MethodName="Command"/>
</i:EventTrigger.Actions>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Is there a reason why this can't be done?
Thanks
I have created an attached property attaching the Caliburn Message to the MahApps TextBox button.
public static class MahappsHelper
{
/// <summary>
/// Attach Caliburn cal:Message.Attach for the Mahapps TextBoxHelper.Button
/// </summary>
public static readonly DependencyProperty ButtonMessageProperty =
DependencyProperty.RegisterAttached("ButtonMessage", typeof(string), typeof(MahappsHelper), new PropertyMetadata(null, ButtonMessageChanged));
public static string GetButtonMessage(DependencyObject obj)
{
return (string)obj.GetValue(ButtonMessageProperty);
}
public static void SetButtonMessage(DependencyObject obj, string value)
{
obj.SetValue(ButtonMessageProperty, value);
}
private static void ButtonMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement textBox = d as TextBox;
if (d == null)
textBox = d as PasswordBox;
if (d == null)
textBox = d as ComboBox;
if (textBox == null)
throw new ArgumentException("ButtonMessage does not work with control " + d.GetType());
textBox.Loaded -= ButtonMessageTextBox_Loaded;
Button button = GetTextBoxButton(textBox);
if (button != null)
{
SetButtonMessage(textBox, button);
return;
}
// cannot get button, try it in the Loaded event
textBox.Loaded += ButtonMessageTextBox_Loaded;
}
private static Button GetTextBoxButton(FrameworkElement textBox)
{
return TreeHelper.FindChild<Button>(textBox, "PART_ClearText");
}
private static void ButtonMessageTextBox_Loaded(object sender, RoutedEventArgs e)
{
FrameworkElement textBox = (FrameworkElement)sender;
textBox.Loaded -= ButtonMessageTextBox_Loaded;
Button button = GetTextBoxButton(textBox);
SetButtonMessage(textBox, button);
}
/// <summary>
/// Set Caliburn Message to the TextBox button.
/// </summary>
private static void SetButtonMessage(FrameworkElement textBox, Button button)
{
if (button == null)
return;
string message = GetButtonMessage(textBox);
Caliburn.Micro.Message.SetAttach(button, message);
}
}
And usage:
<TextBox x:Name="SearchBox" mahapps:TextBoxHelper.Watermark="Search ..."
cal:Message.Attach="[KeyDown] = [Search(SearchBox.Text, $eventArgs)]"
my:MahappsHelper.ButtonMessage="Search(SearchBox.Text)" />
This is because it's not an event, you could solve this by turning the call to the command into an attached event then have caliburn watch that event instead.
I'll omit the attached event code, because it's lengthy but can be found here: Custom attached events in WPF
Something like
public class MyControlExtension
{
public static readonly DependencyProperty SendMahAppsCommandAsEventProperty = DependencyProperty.RegisterAttached("SendMahAppsCommandAsEvent", typeof(bool), ... etc ... );
public static SetSendMahAppsCommandAsEvent(DependencyObject element, bool value)
{
if (value)
TextboxHelper.SetButtonCommand(element, CreateCommand(element));
else
TextboxHelper.SetButtonCommand(null);
}
public static bool GetHeaderSizingGroup(DependencyObject element)
{
return (bool) element.GetValue(SendMahAppsCommandAsEventProperty);
}
private static ICommand CreateCommand(DependencyObject element)
{
return new MyCommandThatRaisesAttachedEvent(element);
}
}
I have got a big ListBox with vertical scrolling enabled, my MVVM has New and Edit ICommands.
I am adding new item to the end of the collection but I want the scrollbar also to auto position to the End when I call my MVVM-AddCommand.
I am also making an item editable(By calling EditCommand with a particular row item) from some other part of the application so that my ListBoxItem getting in to edit mode using DataTrigger, but how will I bring that particular row(ListBoxItem) to the view by adjusting the scroll position.
If I am doing it in the View side I can call listBox.ScrollInToView(lstBoxItem).
But what is the best way to solve this common Scroll issue from an MVVM perspective.
I typically set IsSynchronizedWithCurrentItem="True" on the ListBox. Then I add a SelectionChanged handler and always bring the selected item into view, with code like this:
private void BringSelectionIntoView(object sender, SelectionChangedEventArgs e)
{
Selector selector = sender as Selector;
if (selector is ListBox)
{
(selector as ListBox).ScrollIntoView(selector.SelectedItem);
}
}
From my VM I can get the default collection view and use one of the MoveCurrent*() methods to ensure that the item being edited is the current item.
CollectionViewSource.GetDefaultView(_myCollection).MoveCurrentTo(thisItem);
NOTE: Edited to use ListBox.ScrollIntoView() to accomodate virtualization
Using this in MVVM can be easily accomplished via an attached behavior like so:
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace Jarloo.Sojurn.Behaviors
{
public sealed class ScrollIntoViewBehavior : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += ScrollIntoView;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= ScrollIntoView;
base.OnDetaching();
}
private void ScrollIntoView(object o, SelectionChangedEventArgs e)
{
ListBox b = (ListBox) o;
if (b == null)
return;
if (b.SelectedItem == null)
return;
ListBoxItem item = (ListBoxItem) ((ListBox) o).ItemContainerGenerator.ContainerFromItem(((ListBox) o).SelectedItem);
if (item != null) item.BringIntoView();
}
}
}
Then in the View ad this reference at the top:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
And do this:
<ListBox ItemsSource="{Binding MyData}" SelectedItem="{Binding MySelectedItem}">
<i:Interaction.Behaviors>
<behaviors:ScrollIntoViewBehavior />
</i:Interaction.Behaviors>
</ListBox>
Now when the SelectedItem changes the behavior will do the BringIntoView() call for you.
This is the attached property form of the accepted answer:
using System.Windows;
using System.Windows.Controls;
namespace CommonBehaviors
{
public static class ScrollCurrentItemIntoViewBehavior
{
public static readonly DependencyProperty AutoScrollToCurrentItemProperty =
DependencyProperty.RegisterAttached("AutoScrollToCurrentItem",
typeof(bool), typeof(ScrollCurrentItemIntoViewBehavior),
new UIPropertyMetadata(default(bool), OnAutoScrollToCurrentItemChanged));
public static bool GetAutoScrollToCurrentItem(DependencyObject obj)
{
return (bool)obj.GetValue(AutoScrollToCurrentItemProperty);
}
public static void OnAutoScrollToCurrentItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var listBox = obj as ListBox;
if (listBox == null) return;
var newValue = (bool)e.NewValue;
if (newValue)
listBox.SelectionChanged += listBoxSelectionChanged;
else
listBox.SelectionChanged -= listBoxSelectionChanged;
}
static void listBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBox = sender as ListBox;
if (listBox == null || listBox.SelectedItem == null || listBox.Items == null) return;
listBox.Items.MoveCurrentTo(listBox.SelectedItem);
listBox.ScrollIntoView(listBox.SelectedItem);
}
public static void SetAutoScrollToCurrentItem(DependencyObject obj, bool value)
{
obj.SetValue(AutoScrollToCurrentItemProperty, value);
}
}
}
Usage:
<ListBox ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True"
behaviors:ScrollCurrentItemIntoViewBehavior.AutoScrollToCurrentItem="True">
If the above code doesn't work for you, give this a try
public class ListBoxExtenders : DependencyObject
{
public static readonly DependencyProperty AutoScrollToCurrentItemProperty = DependencyProperty.RegisterAttached("AutoScrollToCurrentItem", typeof(bool), typeof(ListBoxExtenders), new UIPropertyMetadata(default(bool), OnAutoScrollToCurrentItemChanged));
public static bool GetAutoScrollToCurrentItem(DependencyObject obj)
{
return (bool)obj.GetValue(AutoScrollToSelectedItemProperty);
}
public static void SetAutoScrollToCurrentItem(DependencyObject obj, bool value)
{
obj.SetValue(AutoScrollToSelectedItemProperty, value);
}
public static void OnAutoScrollToCurrentItemChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
{
var listBox = s as ListBox;
if (listBox != null)
{
var listBoxItems = listBox.Items;
if (listBoxItems != null)
{
var newValue = (bool)e.NewValue;
var autoScrollToCurrentItemWorker = new EventHandler((s1, e2) => OnAutoScrollToCurrentItem(listBox, listBox.Items.CurrentPosition));
if (newValue)
listBoxItems.CurrentChanged += autoScrollToCurrentItemWorker;
else
listBoxItems.CurrentChanged -= autoScrollToCurrentItemWorker;
}
}
}
public static void OnAutoScrollToCurrentItem(ListBox listBox, int index)
{
if (listBox != null && listBox.Items != null && listBox.Items.Count > index && index >= 0)
listBox.ScrollIntoView(listBox.Items[index]);
}
}
Usage in XAML
<ListBox IsSynchronizedWithCurrentItem="True" extenders:ListBoxExtenders.AutoScrollToCurrentItem="True" ..../>