Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically updating WPF data template
    primarykey
    data
    text
    <p>I have the below Button XAML</p> <pre><code>&lt;Button Name="btnLogoff" DockPanel.Dock="Left" Click="btnToolbar_Click" CommandParameter="LOGOFF" Template="{DynamicResource ToolbarButton}" Style="{StaticResource ToolbarButtonDisplay}" z:ButtonProperties.Image="..\..\Resources\Images\Logoff.GIF" Content="Logoff"&gt; &lt;/Button&gt; </code></pre> <p>ButtonProperties is as follows</p> <pre><code>public class ButtonProperties { public static ImageSource GetImage(DependencyObject obj) { return (ImageSource) obj.GetValue(ImageProperty); } public static void SetImage(DependencyObject obj, ImageSource value) { obj.SetValue(ImageProperty, value); } public static readonly DependencyProperty ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(ButtonProperties), new UIPropertyMetadata((ImageSource)null)); } </code></pre> <p>Which works fine, to give me a template for buttons I can use throughout the app that have an image and text component.</p> <p>What I want to be able to do is change the style dynamically from ToolbarButton to ToolbarButtonRed on certain background thread methods, such as notifying the user that an event is about to occur, the button will change to a red style. Both styles are in the resources of the XAML page but I cant figure out how to change the Template programmatically.</p> <p>I have tried..</p> <pre><code> public DataTemplate AlarmTemplate { get { if (_alarmTemplate != null) return _alarmTemplate; else return (DataTemplate)_parent.FindResource("ToolbarButton"); } set { _alarmTemplate = value; OnPropertyChanged("AlarmTemplate"); } } private void SetAlarmIcon(bool red) { if (red) AlarmTemplate = (DataTemplate)_parent.FindResource("ToolbarButtonRed"); else AlarmTemplate = (DataTemplate)_parent.FindResource("ToolbarButton"); } </code></pre> <p>and binding the XAML as such</p> <p> </p> <p>But no joy. The button doesnt even display at all. Any suggestions?</p> <p>UPDATE:</p> <p>Style below</p> <pre><code>&lt;Style x:Key="ToolbarButtonDisplay" TargetType="Button"&gt; &lt;Setter Property="ContentTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;Image Source="{Binding Path=(Windows:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Margin="0,2,0,0" Width="32"/&gt; &lt;TextBlock FontSize="9" FontWeight="Normal" FontFamily="Arial" Foreground="White" HorizontalAlignment="Center" Margin="2,2,2,2"&gt; &lt;ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"&gt;&lt;/ContentPresenter&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsEnabled}" Value="True"&gt; &lt;Setter Property="Template" Value="{StaticResource ToolbarButtonRed}"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>Final update - I got a trigger to work as below</p> <pre><code>&lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=DisplayToolbar}" Value="True"&gt; &lt;Setter Property="Template" Value="{DynamicResource ToolbarButtonRed}"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; </code></pre> <p>By just using an arbitrary property on the datacontext (DisplayToolbar) as I am not sure how to do this specific to a button. To explain, I have about 30 buttons on the page, which all use the same style and hence the same trigger so the above code will set all 30 to red. I need a way for each button to workout whether IT and IT ALONE should be red, so I'm thinking along the lines of setting some property of a button control if there is one I can use, and have the data trigger look at the buttons properties, not the datacontexts. What is the syntax to bind to the buttons own properties rather than the datacontext?</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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