2012-12-05 53 views
1

如何控制属性设置为特定值,如果同一控件的事件触发?如何让控件在事件触发时设置属性

比方说,我有一个扩展

<Expander Header="Click to expand" GotFocus="IsExpanded=True" /> 

而且我想给IsExpanded属性设置为true,如果它得到了焦点。 如何在Xaml中做到这一点?

回答

3

你可以尝试使用binding,大概是这样的:

<Expander IsExpanded="{Binding IsFocused, RelativeSource={RelativeSource Self}, Mode=OneWay}" /> 
2

阿德里安的做法是达到目标的最彻底的方法。但是,如果你想改变一个属性当事件触发,你可以试试这个:

<Expander Header="Click to expand"> 
    <Expander.Style> 
    <Style TargetType="Expander"> 
     <Style.Triggers> 
     <EventTrigger RoutedEvent="GotFocus"> 
      <BeginStoryboard> 
      <Storyboard> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Expander.IsExpanded)"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="True"/> 
       </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
     </Style.Triggers> 
    </Style> 
    </Expander.Style> 
</Expander> 

注:这纯粹是从内存,并且可能无法正常工作原样。但它应该给你一个关于如何完成的好主意。

+0

我试着使用事件* MouseEnter *而不是* GotFocus *(因为它更容易触发;)),并且我得到* System.Windows.Media.Animation.AnimationStorage.OnCurrentTimeInvalidated *异常。如果我将* KeyTime *的值更改为* 1 *,则不会发生异常,但如果用鼠标触摸扩展器,扩展器不会展开... – clx

相关问题