Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are binding to the <code>DataContext</code> of the <code>Image</code> and not to the templated <code>Button</code></p> <p>Bind to the <code>TemplatedParent</code> instead.</p> <p>On a side note, there is no need to use two <code>DataTriggers</code> for true and false. Do this instead</p> <pre><code>&lt;Button x:Name="ButtonVisible" Tag="1"&gt; </code></pre> <p>And only keep one of the <code>DataTriggers</code></p> <pre><code>&lt;DataTrigger Binding="{Binding Path=IsVisible}" Value="False"&gt; &lt;Setter Property="Tag" TargetName="ButtonVisible" Value="0" /&gt; &lt;/DataTrigger&gt; </code></pre> <hr> <p><strong>Example</strong>, add the following to <code>MainWindow.xaml</code></p> <pre><code>&lt;StackPanel&gt; &lt;StackPanel.Resources&gt; &lt;DataTemplate x:Key="SomeDataTemplateofSomeType"&gt; &lt;Button x:Name="ButtonVisible"&gt; &lt;Button.Template&gt; &lt;ControlTemplate TargetType="Button"&gt; &lt;Image x:Name="FxImage"&gt; &lt;Image.Style&gt; &lt;Style TargetType="{x:Type Image}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource TemplatedParent},UpdateSourceTrigger=PropertyChanged}" Value="0"&gt; &lt;Setter Property="Source" Value="YourImageSource" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Image.Style&gt; &lt;/Image&gt; &lt;/ControlTemplate&gt; &lt;/Button.Template&gt; &lt;/Button&gt; &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsVisible}" Value="True"&gt; &lt;Setter Property="Tag" TargetName="ButtonVisible" Value="1" /&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding Path=IsVisible}" Value="False"&gt; &lt;Setter Property="Tag" TargetName="ButtonVisible" Value="0" /&gt; &lt;/DataTrigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; &lt;/StackPanel.Resources&gt; &lt;ContentControl Content="{Binding MySource}" ContentTemplate="{StaticResource SomeDataTemplateofSomeType}"/&gt; &lt;ToggleButton IsChecked="{Binding Path=MySource.IsVisible, Mode=TwoWay}" Content="IsChecked?"/&gt; &lt;/StackPanel&gt; </code></pre> <p><strong>MainWindow.xaml.cs</strong></p> <pre><code>public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MySource = new MySource(); this.DataContext = this; } public MySource MySource { get; set; } } public class MySource : INotifyPropertyChanged { private bool m_isVisible; public bool IsVisible { get { return m_isVisible; } set { m_isVisible = value; OnPropertyChanged("IsVisible"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload