2012-11-06 121 views
0

我有下面的按钮XAML动态更新WPF数据模板

<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"> 
       </Button> 

ButtonProperties如下

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)); 
    } 

的正常工作,给我的按钮模板我可以在整个应用程序使用有一个图像和文本组件。

我想要做的就是在某些后台线程方法上动态地将ToolbarButton的样式更改为ToolbarButtonRed,比如通知用户事件即将发生,按钮将变为红色样式。这两种样式都在XAML页面的资源中,但我不知道如何以编程方式更改模板。

我已经试过..

 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"); 
     } 

并绑定XAML这样

,但没有喜悦。该按钮甚至不显示。有什么建议么?

UPDATE:

风格低于

<Style x:Key="ToolbarButtonDisplay" TargetType="Button"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <Image Source="{Binding Path=(Windows:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Margin="0,2,0,0" Width="32"/> 
         <TextBlock FontSize="9" FontWeight="Normal" FontFamily="Arial" Foreground="White" HorizontalAlignment="Center" Margin="2,2,2,2"> 
           <ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></ContentPresenter> 
         </TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=IsEnabled}" Value="True"> 
       <Setter Property="Template" Value="{StaticResource ToolbarButtonRed}"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

最后更新 - 我有一个触发如下

<Style.Triggers> 
      <DataTrigger Binding="{Binding Path=DisplayToolbar}" Value="True"> 
       <Setter Property="Template" Value="{DynamicResource ToolbarButtonRed}"/> 
      </DataTrigger> 
     </Style.Triggers> 

上班只要使用上的datacontext(DisplayToolbar)任意财产我不知道如何做到这一点特定于一个按钮。为了解释,我在页面上有大约30个按钮,它们都使用相同的样式,因此使用相同的触发器,因此上面的代码将全部设置为红色。我需要一种方法来让每个按钮锻炼IT和IT是否应该是红色的,所以我正在考虑设置按钮控件的某些属性(如果有我可以使用的),并让数据触发器查看按钮属性,而不是datacontexts。什么是绑定到按钮自己的属性,而不是datacontext的语法?

回答

0

它应该与将Style属性设置为FindResource中的值一样简单。

Dispatcher.Invoke(() => { btnLogoff.Style = (Style)FindResource("ToolbarButtonRed"); }); 

调度程序调用在此处使用,因为您无法在后台线程的控件上设置依赖项属性。

根据以下评论,可以完全相同的方式设置控件模板。

Dispatcher.Invoke(() => { btnLogoff.Template = (ControlTemplate)FindResource("ToolbarButtonRed"); }); 
+0

对不起,我应该指定的ToolBarButton和ToolbarButtonRed是CONTROLTEMPLATES,没有风格 – NZJames

+0

如果我理解正确的问题,编辑应该是你所需要的。 – Andy

+0

我试图通过MVVM做到这一点,所以如果可能的话避免直接的btn属性更改,最好是更新一个依赖项属性,然后会影响GUI。你可以不使用调度程序调用来更新GUI线程上的依赖项属性吗?因为这是我最初的做法,但那并不奏效。另外,我已经发布了上面的按钮样式定义,并通过数据触发器添加到块中,这可能吗?我做了什么错误,因为它似乎没有工作。 – NZJames