Hi,
I have a WPF Graph with plots, and a Legend.
The plots of Graph binded to Legend.
I would like manipulate the IsEnable property of Legend items if the number of visible plots greather then 5.
Example:
I have 10 pcs plot, and the default value of all plot's visible property is collapsed.
I would like set a restriction to the number of visible plots.
If I enable the plots visibility with the toggle button of the Legend, and the visible plots number is equal 5, then the IsEnable property of toggle buttons of all invisible plots set be false.
If the number of visibled plots decrase below 5, then all plot IsEnable property set be true.
So, the question is, how can i manipulate the Legend items or Toggle Buttons IsEnabled property from ViewModel?
Thanks before!
Regards,
Tamas
MainWindow.xaml
<ni:Graph x:Name="graph" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" RenderMode="Vector" local:GraphExtensions.PlotsSource="{Binding PlotCollection}"><ni:Graph.Resources><Style TargetType="{x:Type ni:Plot}"><Setter Property="LabelTemplate" Value="{StaticResource ReadOnlyPlotLabelTemplate}" /></Style></ni:Graph.Resources></ni:Graph><GroupBox Grid.Column="3" Grid.Row="1" Header="Plots"><ni:Legend x:Name="legend" ItemsSource="{Binding ElementName=graph, Path=Plots}" /></GroupBox>
MainWindow.xaml.cs
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new ViewModel(graph, legend); } }
ViewModel.cs
public ObservableCollection<Plot> PlotCollection { get; set; } private Graph g; private Legend l; private DispatcherTimer dt; public ViewModel(Graph _g, Legend _l) { g = _g; l = _l; PlotCollection = new ObservableCollection<Plot>(); dt = new DispatcherTimer(); dt.Interval = TimeSpan.FromMilliseconds(250); dt.Tick += dt_Tick; dt.Start(); } void dt_Tick(object sender, EventArgs e) { int visibleCount = (from v in g.Plots where (v.Visibility == Visibility.Visible) select v).Count(); if (visibleCount >= 5) { foreach (Plot item in l.Items) {
if(item.Visibility == Visibility.Collapsed) item.IsEnabled = false; } return; } else { foreach (var item in l.Items) { (item as Plot).IsEnabled = false; } } }