forked from telerik/xaml-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c67d68b
commit 9c4bcb4
Showing
17 changed files
with
264 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## Selected Items Binding ## | ||
##Selected Items Binding## | ||
This example demonstrates how to bind the selected items of the RadComboBox control to a property in your ViewModel class. | ||
|
||
<keywords:selected, items, binding> | ||
<keywords:selected, items, binding> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<Application x:Class="WpfApplication1.App" | ||
<Application x:Class="BindingSelectedItemsFromViewModel.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
<Application.Resources> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
x:Class="SilverlightApplication1.App" | ||
> | ||
<Application.Resources> | ||
|
||
x:Class="BindingSelectedItemsFromViewModel.App"> | ||
<Application.Resources> | ||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<UserControl x:Class="BindingSelectedItemsFromViewModel.Example" | ||
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:BindingSelectedItemsFromViewModel" | ||
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" | ||
mc:Ignorable="d" | ||
d:DesignHeight="300" d:DesignWidth="400"> | ||
<Grid x:Name="LayoutRoot" Background="White"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition /> | ||
<RowDefinition Height="30" /> | ||
<RowDefinition /> | ||
</Grid.RowDefinitions> | ||
<telerik:RadGridView ItemsSource="{Binding View}" SelectionMode="Extended" Margin="5" | ||
local:GridViewSelectionUtilities.SelectedItems="{Binding SelectedItems}"> | ||
<telerik:RadGridView.Columns> | ||
<telerik:GridViewSelectColumn /> | ||
</telerik:RadGridView.Columns> | ||
</telerik:RadGridView> | ||
<TextBlock Text="SelectedItems:" Margin="5" Grid.Row="1" /> | ||
<ListBox ItemsSource="{Binding SelectedItems}" DisplayMemberPath="ID" Grid.Row="2" Margin="5" /> | ||
</Grid> | ||
</UserControl> |
14 changes: 14 additions & 0 deletions
14
GridView/BindingSelectedItemsFromViewModel/Example.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace BindingSelectedItemsFromViewModel | ||
{ | ||
public partial class Example : UserControl | ||
{ | ||
public Example() | ||
{ | ||
InitializeComponent(); | ||
|
||
DataContext = new MyDataContext(); | ||
} | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
GridView/BindingSelectedItemsFromViewModel/GridViewSelectionUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using System.Windows; | ||
using Telerik.Windows.Controls; | ||
|
||
namespace BindingSelectedItemsFromViewModel | ||
{ | ||
public static class GridViewSelectionUtilities | ||
{ | ||
private static bool isSyncingSelection; | ||
private static List<Tuple<WeakReference, List<RadGridView>>> collectionToGridViews = new List<Tuple<WeakReference, List<RadGridView>>>(); | ||
|
||
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached( | ||
"SelectedItems", | ||
typeof(INotifyCollectionChanged), | ||
typeof(GridViewSelectionUtilities), | ||
new PropertyMetadata(null, OnSelectedItemsChanged)); | ||
|
||
public static INotifyCollectionChanged GetSelectedItems(DependencyObject obj) | ||
{ | ||
return (INotifyCollectionChanged)obj.GetValue(SelectedItemsProperty); | ||
} | ||
|
||
public static void SetSelectedItems(DependencyObject obj, INotifyCollectionChanged value) | ||
{ | ||
obj.SetValue(SelectedItemsProperty, value); | ||
} | ||
|
||
private static void OnSelectedItemsChanged(DependencyObject target, DependencyPropertyChangedEventArgs args) | ||
{ | ||
var gridView = (RadGridView)target; | ||
|
||
var oldCollection = args.OldValue as INotifyCollectionChanged; | ||
if (oldCollection != null) | ||
{ | ||
gridView.SelectionChanged -= GridView_SelectionChanged; | ||
oldCollection.CollectionChanged -= SelectedItems_CollectionChanged; | ||
RemoveAssociation(oldCollection, gridView); | ||
} | ||
|
||
var newCollection = args.NewValue as INotifyCollectionChanged; | ||
if (newCollection != null) | ||
{ | ||
gridView.SelectionChanged += GridView_SelectionChanged; | ||
newCollection.CollectionChanged += SelectedItems_CollectionChanged; | ||
AddAssociation(newCollection, gridView); | ||
OnSelectedItemsChanged(newCollection, null, (IList)newCollection); | ||
} | ||
} | ||
|
||
private static void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args) | ||
{ | ||
INotifyCollectionChanged collection = (INotifyCollectionChanged)sender; | ||
OnSelectedItemsChanged(collection, args.OldItems, args.NewItems); | ||
} | ||
|
||
private static void GridView_SelectionChanged(object sender, SelectionChangeEventArgs args) | ||
{ | ||
if (isSyncingSelection) | ||
{ | ||
return; | ||
} | ||
|
||
var collection = (IList)GetSelectedItems((RadGridView)sender); | ||
foreach (object item in args.RemovedItems) | ||
{ | ||
collection.Remove(item); | ||
} | ||
foreach (object item in args.AddedItems) | ||
{ | ||
collection.Add(item); | ||
} | ||
} | ||
|
||
private static void OnSelectedItemsChanged(INotifyCollectionChanged collection, IList oldItems, IList newItems) | ||
{ | ||
isSyncingSelection = true; | ||
|
||
var gridViews = GetOrCreateGridViews(collection); | ||
foreach (var gridView in gridViews) | ||
{ | ||
SyncSelection(gridView, oldItems, newItems); | ||
} | ||
|
||
isSyncingSelection = false; | ||
} | ||
|
||
private static void SyncSelection(RadGridView gridView, IList oldItems, IList newItems) | ||
{ | ||
if (oldItems != null) | ||
{ | ||
SetItemsSelection(gridView, oldItems, false); | ||
} | ||
|
||
if (newItems != null) | ||
{ | ||
SetItemsSelection(gridView, newItems, true); | ||
} | ||
} | ||
|
||
private static void SetItemsSelection(RadGridView gridView, IList items, bool shouldSelect) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
bool contains = gridView.SelectedItems.Contains(item); | ||
if (shouldSelect && !contains) | ||
{ | ||
gridView.SelectedItems.Add(item); | ||
} | ||
else if (contains && !shouldSelect) | ||
{ | ||
gridView.SelectedItems.Remove(item); | ||
} | ||
} | ||
} | ||
|
||
private static void AddAssociation(INotifyCollectionChanged collection, RadGridView gridView) | ||
{ | ||
List<RadGridView> gridViews = GetOrCreateGridViews(collection); | ||
gridViews.Add(gridView); | ||
} | ||
|
||
private static void RemoveAssociation(INotifyCollectionChanged collection, RadGridView gridView) | ||
{ | ||
List<RadGridView> gridViews = GetOrCreateGridViews(collection); | ||
gridViews.Remove(gridView); | ||
|
||
if (gridViews.Count == 0) | ||
{ | ||
CleanUp(); | ||
} | ||
} | ||
|
||
private static List<RadGridView> GetOrCreateGridViews(INotifyCollectionChanged collection) | ||
{ | ||
for (int i = 0; i < collectionToGridViews.Count; i++) | ||
{ | ||
var wr = collectionToGridViews[i].Item1; | ||
if (wr.Target == collection) | ||
{ | ||
return collectionToGridViews[i].Item2; | ||
} | ||
} | ||
|
||
collectionToGridViews.Add(new Tuple<WeakReference, List<RadGridView>>(new WeakReference(collection), new List<RadGridView>())); | ||
return collectionToGridViews[collectionToGridViews.Count - 1].Item2; | ||
} | ||
|
||
private static void CleanUp() | ||
{ | ||
for (int i = collectionToGridViews.Count - 1; i >= 0; i--) | ||
{ | ||
bool isAlive = collectionToGridViews[i].Item1.IsAlive; | ||
var behaviors = collectionToGridViews[i].Item2; | ||
if (behaviors.Count == 0 || !isAlive) | ||
{ | ||
collectionToGridViews.RemoveAt(i); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.