0
votes

Hello I have a problem with Binding and DataTrigger. So my code is:

<tog:HorizontalToggleSwitch  Margin="0,10,15,0" HorizontalAlignment="Left"  >
<tog:HorizontalToggleSwitch.Style>
  <Style TargetType="{x:Type tog:HorizontalToggleSwitch}">
  <Setter Property="IsChecked" Value="{Binding Staff.isSelfie, Mode=TwoWay}"/>
     <Style.Triggers>
      <DataTrigger Binding="{Binding ElementName=Admin,Path=IsChecked}" Value="True">
        <Setter Property="IsChecked" Value="True" />
        <Setter Property="IsEnabled" Value="False"/>
      </DataTrigger>
     <DataTrigger Binding="{Binding ElementName=Admin,Path=IsChecked}" Value="False">
       <Setter Property="IsChecked" Value="False" />
       <Setter Property="IsEnabled" Value="True"/>
     </DataTrigger>
    </Style.Triggers>
    </Style>
 </tog:HorizontalToggleSwitch.Style>
 </tog:HorizontalToggleSwitch>

As you can see I have a binding value Staff.isSelfie at IsChecked Property and I change it within the Datatrigger. My problem is that I don't get the bool value of IsChecked at my variable Staff.isSelfie. DataTrigger works because the switch change from off to on while I click at Admin Element which is a radiobutton. The code from Staff.isSelfie is:

bool _isSelfie;
    [DataMember]
    public bool isSelfie
    {
        get
        {
            return _isSelfie;
        }

        set
        {
            if (_isSelfie != value)
            {
                _isSelfie = value;
                OnPropertyChanged(nameof(isSelfie));
            }
        }
    }

which implement INotifyPropertyChanged. The variable Staff.isSelfie take his value from a bool in my database thats why I want to set it at the begin. How can I get the value true or false from DataTrigger to my variable Staff.isSelfie? What is wrong? Thanks a lot.

1

1 Answers

0
votes

<Setter Property="IsChecked" Value="False" /> removes existing binding. Try listen events when Admin.IsChecked changes and update HorizontalToggleSwitch.IsChecked accordingly.

HorizontalToggleSwitch will have only trigger for IsEnabled

<tog:HorizontalToggleSwitch IsChecked="{Binding Staff.isSelfie, Mode=TwoWay}"
                            Margin="0,10,15,0" HorizontalAlignment="Left">
<tog:HorizontalToggleSwitch.Style>
  <Style TargetType="{x:Type tog:HorizontalToggleSwitch}">
     <Style.Triggers>
      <DataTrigger Binding="{Binding ElementName=Admin,Path=IsChecked}" Value="True">
        <Setter Property="IsEnabled" Value="False"/>
      </DataTrigger>
     <DataTrigger Binding="{Binding ElementName=Admin,Path=IsChecked}" Value="False">
       <Setter Property="IsEnabled" Value="True"/>
     </DataTrigger>
    </Style.Triggers>
    </Style>
 </tog:HorizontalToggleSwitch.Style>
 </tog:HorizontalToggleSwitch>

and here is an example of changing IsChecked with 2 CheckBoxes:

<CheckBox Content="Admin" Name="Admin" 
          Checked="Admin_CheckedChanged" Unchecked="Admin_CheckedChanged"/>
<CheckBox Name="ToggleSwitch" IsChecked="{Binding Path=isSelfie}" Content="???"/>
private void Admin_CheckedChanged(object sender, RoutedEventArgs e)
{
    ToggleSwitch.SetCurrentValue(CheckBox.IsCheckedProperty, Admin.IsChecked);
}