Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
martinivanoff committed Sep 18, 2015
1 parent c239b6e commit 218024c
Show file tree
Hide file tree
Showing 83 changed files with 2,907 additions and 697 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="ChartUtilities.cs" />
<Compile Include="GridUtilities.cs" />
<Compile Include="DataItem.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
Expand Down
10 changes: 1 addition & 9 deletions ChartView/SL/BindingSelectedItemsToViewModel/ChartUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Charting;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.ChartView;
Expand Down Expand Up @@ -164,7 +156,7 @@ private static IEnumerable GetSeries(RadChartBase chartView)

private static IEnumerable GetDataPoints(ChartSeries series)
{
var dataPoints = (IEnumerable)series.GetType().GetProperty("DataPoints").GetValue(series, null);
var dataPoints = (IEnumerable)series.GetType().GetProperty("DataPoints").GetValue(series, null);
return dataPoints;
}

Expand Down
43 changes: 3 additions & 40 deletions ChartView/SL/BindingSelectedItemsToViewModel/DataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,9 @@

namespace BindingSelectedItemsToViewModel
{
public class DataItem : INotifyPropertyChanged
public class DataItem
{
private bool isSelected;

public double Value
{
get;
set;
}

public string Category
{
get;
set;
}

public bool IsSelected
{
get
{
return this.isSelected;
}
set
{
if (this.isSelected != value)
{
this.isSelected = value;
this.RaisePropertyChanged("IsSelected");
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public double Value { get; set; }
public string Category { get; set; }
}
}
164 changes: 164 additions & 0 deletions ChartView/SL/BindingSelectedItemsToViewModel/GridUtilities.cs
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 BindingSelectedItemsToViewModel
{
public static class GridUtilities
{
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
"SelectedItems",
typeof(INotifyCollectionChanged),
typeof(GridUtilities),
new PropertyMetadata(null, OnSelectedItemsChanged));

private static bool isSyncingSelection;
private static List<Tuple<WeakReference, List<RadGridView>>> collectionToGridViews = new List<Tuple<WeakReference, List<RadGridView>>>();

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 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 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 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);
}
}
}
}
}
14 changes: 4 additions & 10 deletions ChartView/SL/BindingSelectedItemsToViewModel/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>
<FrameworkElement.Resources>
<DataTemplate x:Key="listItemTemplate">
<Border Padding="3"
Margin="5 1"
Expand All @@ -26,9 +26,9 @@
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="5 5 5 0" />
</Style>
</UserControl.Resources>
</FrameworkElement.Resources>

<Grid x:Name="LayoutRoot" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
Expand Down Expand Up @@ -77,13 +77,7 @@
Grid.Row="1"
ItemsSource="{Binding CartesianDataItems}"
SelectionMode="Multiple"
SelectionChanged="gridView_SelectionChanged">
<telerik:RadGridView.Resources>
<Style TargetType="telerik:GridViewRow">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</telerik:RadGridView.Resources>
</telerik:RadGridView>
local:GridUtilities.SelectedItems="{Binding SelectedCartesianDataItems}"/>

<StackPanel Grid.Column="2">
<TextBlock Text="Selected Pie Data Items" Style="{StaticResource listHeaderStyle}" />
Expand Down
42 changes: 2 additions & 40 deletions ChartView/SL/BindingSelectedItemsToViewModel/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace BindingSelectedItemsToViewModel
{
public partial class MainPage : UserControl
{
private MainViewModel viewModel;

public MainPage()
{
InitializeComponent();
this.viewModel = new MainViewModel();
this.DataContext = this.viewModel;
}

private void gridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
{
if (e.AddedItems != null)
{
foreach (DataItem item in e.AddedItems)
{
if (!this.viewModel.SelectedCartesianDataItems.Contains(item))
{
this.viewModel.SelectedCartesianDataItems.Add(item);
}
}
}

if (e.RemovedItems != null)
{
foreach (DataItem item in e.RemovedItems)
{
if (this.viewModel.SelectedCartesianDataItems.Contains(item))
{
this.viewModel.SelectedCartesianDataItems.Remove(item);
}
}
}
this.DataContext = new MainViewModel();
}
}
}
29 changes: 4 additions & 25 deletions ChartView/SL/BindingSelectedItemsToViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,23 @@ namespace BindingSelectedItemsToViewModel
public class MainViewModel
{
private Random randomNumberGenerator = new Random(0);

public ObservableCollection<DataItem> CartesianDataItems { get; set; }
public ObservableCollection<DataItem> PieDataItems { get; set; }

public ObservableCollection<DataItem> SelectedPieDataItems { get; set; }
public ObservableCollection<DataItem> SelectedCartesianDataItems { get; set; }
public ObservableCollection<DataItem> SelectedPieDataItems { get; set; }

public MainViewModel()
{
this.CartesianDataItems = GetRandomData(10);
this.PieDataItems = GetRandomData(5);

this.SelectedPieDataItems = new ObservableCollection<DataItem>();
this.SelectedCartesianDataItems = new ObservableCollection<DataItem>();

this.SelectedCartesianDataItems.CollectionChanged += SelectedCartesianDataItems_CollectionChanged;

this.PieDataItems = GetRandomData(5);
this.SelectedPieDataItems = new ObservableCollection<DataItem>();
this.SelectedPieDataItems.Add(this.PieDataItems.Last());
}

void SelectedCartesianDataItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (DataItem item in e.OldItems)
{
item.IsSelected = false;
}
}

if (e.NewItems != null)
{
foreach (DataItem item in e.NewItems)
{
item.IsSelected = true;
}
}
}

private ObservableCollection<DataItem> GetRandomData(int itemsCount)
{
var result = new ObservableCollection<DataItem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="ChartUtilities.cs" />
<Compile Include="GridUtilities.cs" />
<Compile Include="DataItem.cs" />
<Compile Include="MainViewModel.cs" />
<Compile Include="MainWindow.xaml.cs">
Expand Down
Loading

0 comments on commit 218024c

Please sign in to comment.