Search code examples
c#bindinglivecharts

Bind Pie Series to a checkbox - c# (LiveCharts)


I have a simple program with a checkbox, textbox, and a pieChart.

I want when i press the checkbox the color and text of the textbox will be change

as well as some property of the pieChart.

When i tried to change to the property to pieChart itself it worked fine.

But i try to change a property inside it's collection im getting an error

Cannot find source for binding with reference

My Code

<Window x:Class="TestLiveCharts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        x:Name="UC">   


    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <CheckBox x:Name="cbSample" Content="Hello" Grid.Row="0" />
        <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48"  Grid.Row="1">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        //--> this is where i get my error
                        <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True"> 
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <lvc:PieChart  Grid.Row="2" x:Name="chart" Hoverable="True" HoverPushOut="6" >

                <lvc:PieChart.Series>
                <lvc:PieSeries Values="1"  Fill="Black"  StrokeThickness="0">
                    <lvc:PieSeries.Style>
                        <Style TargetType="lvc:PieSeries">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource  AncestorType={x:Type CheckBox}}, Path=IsChecked}" Value="True">
                                    <Setter Property="StrokeThickness" Value="10"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </lvc:PieSeries.Style>
                </lvc:PieSeries>
                    <lvc:PieSeries Values="1"  Fill="Green"  StrokeThickness="0"/>
                    <lvc:PieSeries Values="1"  Fill="Yellow"   StrokeThickness="0"/>
                    <lvc:PieSeries Values="1"  Fill="DarkCyan"   StrokeThickness="0"/>
                    <lvc:PieSeries Values="1"  Fill="Gray"   StrokeThickness="0"/>
                </lvc:PieChart.Series>
            </lvc:PieChart>

    </Grid>
</Window>

Solution

  • I would bind the IsChecked property of the checkbox to a value in your viewmodel, and bind your datatrigger to this same value in the viewmodel.

    Alternatively, binding to the ElementName works in both cases - try this:

    <Window x:Class="TestLiveCharts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        x:Name="UC">
    
    
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <CheckBox x:Name="cbSample" Content="Hello" Grid.Row="0" />
        <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48"  Grid.Row="1">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <lvc:PieChart  Grid.Row="2" x:Name="chart" Hoverable="True" HoverPushOut="6" >
            <lvc:PieChart.Series>
                <lvc:PieSeries Values="1" >
                    <lvc:PieSeries.Style>
                        <Style TargetType="lvc:PieSeries">
                            <Setter Property="StrokeThickness" Value="0"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
                                    <Setter Property="StrokeThickness" Value="10"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </lvc:PieSeries.Style>
                </lvc:PieSeries>
                <lvc:PieSeries Values="1"  Fill="Green"  StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="Yellow"   StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="DarkCyan"   StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="Gray"   StrokeThickness="0"/>
            </lvc:PieChart.Series>
        </lvc:PieChart>
    
    </Grid>
    

    Clicking the check box will give you this result: enter image description here

    Or try by binding to viewmodel:

    <Window x:Class="StackoverflowTest.MainWindow"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
    x:Name="UC">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <CheckBox x:Name="cbSample" Content="Hello" Grid.Row="0" IsChecked="{Binding Checked}"/>
        <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48"  Grid.Row="1">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Text" Value="No" />
                    <Setter Property="Foreground" Value="Red" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Checked}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                            <Setter Property="Foreground" Value="Green" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <lvc:PieChart  Grid.Row="2" x:Name="chart" Hoverable="True" HoverPushOut="6" >
            <lvc:PieChart.Series>
                <lvc:PieSeries Values="1" >
                    <lvc:PieSeries.Style>
                        <Style TargetType="lvc:PieSeries">
                            <Setter Property="StrokeThickness" Value="0"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Checked}" Value="True">
                                    <Setter Property="StrokeThickness" Value="10"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </lvc:PieSeries.Style>
                </lvc:PieSeries>
                <lvc:PieSeries Values="1"  Fill="Green"  StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="Yellow"   StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="DarkCyan"   StrokeThickness="0"/>
                <lvc:PieSeries Values="1"  Fill="Gray"   StrokeThickness="0"/>
            </lvc:PieChart.Series>
        </lvc:PieChart>
    
    </Grid>
    

    using System.ComponentModel;
    using System.Windows;
    
    namespace StackoverflowTest
    {
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            bool _checked = true;
            public bool Checked
            {
                get
                {
                    return _checked;
                }
                set
                {
                    _checked = value;
                    RaisePropertyChanged(nameof(Checked));
                }
            }
    
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }