Dynamic Visibility
The [ObservableObject]
, [ObservableProperty]
and [RelayCommand]
attributes come from the
CommunityToolkit.Mvvm package, you can read more about it
here.
View model
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
namespace ViewModelsSamples.General.Visibility;
public partial class ViewModel : ObservableObject
{
public ISeries[] Series { get; set; } =
{
new ColumnSeries<double>
{
Values = new ObservableCollection<double> { 2, 5, 4, 3 },
IsVisible = true
},
new ColumnSeries<double>
{
Values = new ObservableCollection<double> { 6, 3, 2, 8 },
IsVisible = true
},
new ColumnSeries<double>
{
Values = new ObservableCollection<double> { 4, 2, 8, 7 },
IsVisible = true
}
};
[RelayCommand]
public void ToggleSeries0()
{
Series[0].IsVisible = !Series[0].IsVisible;
}
[RelayCommand]
public void ToggleSeries1()
{
Series[1].IsVisible = !Series[1].IsVisible;
}
[RelayCommand]
public void ToggleSeries2()
{
Series[2].IsVisible = !Series[2].IsVisible;
}
}
XAML
<UserControl
x:Class="UnoWinUISample.General.Visibility.View"
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:lvc="using:LiveChartsCore.SkiaSharpView.WinUI"
xmlns:vms="using:ViewModelsSamples.General.Visibility"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Command="{Binding ToggleSeries0Command}" Margin="8 0">Toggle first</Button>
<Button Command="{Binding ToggleSeries1Command}" Margin="8 0">Toggle second</Button>
<Button Command="{Binding ToggleSeries2Command}" Margin="8 0">Toggle third</Button>
</StackPanel>
<lvc:CartesianChart Grid.Row="1" Series="{Binding Series}" />
</Grid>
</UserControl>