如何通过xaml更改动态资源值(例如触发器)

时间:2012-05-02 08:42:18

标签: wpf xaml triggers dynamicresource

例如,我有元素的样式,并且我想要更改特定资源的触发器

<Style x:Key="MyStyle" TargetType="{x:Type Tree:MyListBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Property}" Value="ItemSelected">
           <Setter Property="MydinamicResourceKey" Value="NewValue"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

有可能吗?

1 个答案:

答案 0 :(得分:0)

也许尝试使用VisualStateManager?

这是来自Microsoft's ListBox Styles and Templates page

的想法
<Style x:Key="MyStyle" TargeType="{x:Type Tree:MyListBox}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Tree:MyListBox}">
         <Border x:Name="Border">
           <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="SelectionStates">
                 <VisualState x:Name="Unselected"/>
                 <VisualState x:Name="Selected">
                    <Storyboard>
                       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="myUIElementName"
                                                      Storyboard.TargetProperty="myUIElementProperty">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{DynamicResource MyResourceKey}"/>
                        </ObjectAnimationUsingKeyFrames>
                       </Storyboard>
                      </VisualState>
                     </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    .... more code here....
                  </Border>
                 </ControlTemplate>
                </Setter.Value>
               </Setter>
             </Style>

我对这种类型的动画/变化有一个tutorial by Shawn Wildermuth bookmarked - 我仍然会参考它。

希望这很有用,可以让你朝着正确的方向前进。