05-08-2018 08:13 AM
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; } } }
Solved! Go to Solution.
05-09-2018 10:09 AM
The buttons in the legend are generated from the data template of the associated item (plots, in this case). While it is possible to dig through the visual tree from the view model to retrieve these buttons, it would be simpler to bind the view to the IsEnabled
property you are already modifying on the plot. Using the example data template from your earlier question, you just need to add an IsEnabled="{Binding IsEnabled}"
to the Button
.
(Note that the timer-based polling is more aggressive than necessary. If you are still using the event handlers from the earlier question, then you already have a good place to trigger the IsEnabled
update. If not, you could listen to the Visibility
property on each plot with a binding, or you could dispatch from a preview routed event, to know when an update is needed.)
05-10-2018 07:05 AM
Thank you!
And the solution was here before me. 😄