title: Wpf Button conditional binding to a variable
body: <p>I'm new to WPF and I am facing an issue that I am not quite sure how to solve. I would appreciate it if someone can guide me to the best way to solve this.</p>
  
  <ul>
  <li><p>I have a variable that I get from a PLC, this variable's values can be 0, 1, 2 and 3.</p></li>
  <li><p>I have 4 toggle buttons, which have to be binded to that same variable.</p></li>
  <li>When the variable's value is 0, the first button should be checked and the others unchecked.</li>
  <li>When the variable's value is 1, the second button should be checked, and so on.</li>
  </ul>
  
  <p>This binding has to be a two way binding, so if I press one of the buttons, the variable's value has to change.</p>
  
  <blockquote>
    <p>I have 3 main constraints : </p>
    
    <ol>
    <li><p>I am looking for a XAML only solution. I am wondering if this can be done in XAML alone.</p></li>
    <li><p>The button I am using is a custom one, from a closed source framework.</p></li>
    <li><p>I am only able to bind the button to the variable's name, which is a string so I am not able to set a converter for it for example.</p></li>
    </ol>
  </blockquote>
  
  <p>For simplicity, I only wrote the relevant parts of the code, since I'm not looking to figure out why my code's not working as much as I'm looking for a better approach to solve this. So basically some ideas about what should be done so that I can learn about that and fix it myself.</p>
  
  <p>I have tried 2 solutions so far.</p>
  
  <ul>
  <li>First solution :</li>
  </ul>
  
  <p>Adding a Textbox, binding the textbox to the variable and trying to bind the datatrigger to the textbox instead.</p>
  
  <p>The main issue with this solution is that the TwoWay binding isn't working for some reason, the buttons' checked property change when the variable's value changes but pressing the buttons does nothing.</p>
  
  <pre><code>&lt;TextBox Name="myTextBox" height="0" PlcVarName="varName" /&gt; 
  &lt;StackPanel&gt;
          &lt;CustomToggleButton&gt;
              &lt;CustomToggleButton.Style&gt;
                  &lt;Style TargetType="{x:Type CustomToggleButton}"&gt;
                      &lt;Style.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding ElementName=myTextBox, Path=Value, Mode=TwoWay} Value="0"&gt;
                              &lt;Setter Property="IsChecked" Value="true"/&gt;
                          &lt;/DataTrigger&gt;
                          &lt;DataTrigger Binding="{Binding ElementName=myTextBox, Path=Value, Mode=TwoWay} Value="1"&gt;
                              &lt;Setter Property="IsChecked" Value="false"/&gt;
                          &lt;/DataTrigger&gt;
  &lt;/DataTrigger&gt;
                          &lt;DataTrigger Binding="{Binding ElementName=myTextBox, Path=Value, Mode=TwoWay} Value="2"&gt;
                              &lt;Setter Property="IsChecked" Value="false"/&gt;
                          &lt;/DataTrigger&gt;
  &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding ElementName=myTextBox, Path=Value, Mode=TwoWay} Value="3"&gt;
                                  &lt;Setter Property="IsChecked" Value="false"/&gt; 
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/CustomToggleButton.Style&gt;
              &lt;/CustomToggleButton &gt;
  </code></pre>
  
  <p>Same for the other 4 buttons but for button2 it will be checked when value is 1 and so on.</p>
  
  <p>The second solution is as follow :</p>
  
  <p>To give you a better idea, I am able to bind the button to the variable this way :</p>
  
  <pre><code> &lt;CustomToggleButton PlcVarName="varName"&gt;
  </code></pre>
  
  <p>(unnecessary properties are omitted for simplicity)</p>
  
  <p>For a toggleButton ( or a button) of a 2 state variable only (true/false) this binding works perfectly, but now the issue is that my variable has 4 states.</p>
  
  <p>So I was thinking about having a data trigger that binds to whatever the button is binded to. So something like this :</p>
  
  <pre><code>&lt;DataTrigger Binding="{Binding} Value="0"&gt;
                              &lt;Setter Property="IsChecked" Value="true"/&gt;
                          &lt;/DataTrigger&gt;
  </code></pre>
  
  <p>To bind it to itself. or using: </p>
  
  <pre><code> "{Binding RelativeSource={RelativeSource Self} 
  </code></pre>
  
  <p>but both did not work.</p>