2012-07-27 101 views
3

在我的WPF控件,我有以下两个触发器:如何将触发器与SourceName和DataTrigger结合使用?

<Trigger 
    Property="Controls:TreeViewExItem.IsMouseOver" 
    Value="True" 
    SourceName="ElementGrid"> 

<DataTrigger 
    Binding="{Binding 
    RelativeSource={RelativeSource AncestorType={x:Type Controls:TreeViewEx}}, 
    Path=HoverHighlighting}" 
    Value="False"> 

两个为自己工作的罚款。但我需要这些的组合。我试过这个:

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
    <Condition 
     Binding="{Binding 
     RelativeSource={RelativeSource AncestorType={x:Type Controls:TreeViewEx}}, 
     Path=HoverHighlighting}" 
     Value="True"/> 
    <Condition 
     Binding="{Binding 
     (Controls:TreeViewExItem.IsMouseOver), 
     Source=ElementGrid}" 
     Value="True"/> 
    </MultiDataTrigger.Conditions> 

但它什么也没做。我在输出窗口中看到这条消息:

System.Windows.Data Error: 17 : Cannot get 'IsMouseOver' value (type 'Boolean') from '' (type 'String'). BindingExpression:Path=(0); DataItem='String' (HashCode=1047858601); target element is 'TreeViewExItem' (Name=''); target property is 'NoTarget' (type 'Object') InvalidCastException:'System.InvalidCastException: Das Objekt des Typs "System.String" kann nicht in Typ "System.Windows.DependencyObject" umgewandelt werden.

这并没有告诉我任何东西。它将如何工作?

更新:完整的项目代码现在可在我的GitHub存储库中查看。我对MultiDataTrigger的猜测当前位于at

+0

相关问题[这里](http://stackoverflow.com/q/602517/620360)。 – LPL 2012-07-27 11:31:50

+0

相关,但没有帮助,因为它不使用任何SourceName属性。 – ygoe 2012-07-27 15:09:29

回答

1

我已经尝试了很多东西,并没有发现任何工作。直到有人证明我错了,我必须假设Triggers和DataTriggers不能合并。

我的解决方案是另一种:不是试图从同一触发器(它需要不同的触发器类型)访问本地属性和父元素属性,而是将另一个DependencyProperty添加到我的子元素类并将其值绑定到父元素的属性。因此,子元素不需要查找父元素值 - 它始终具有该值本身的当前副本。由于复制该值在另一个位置完成,因此它使触发器保持良好和小巧。 :-)

所以这就是我添加的XAML代码的样子。下面是该子项的风格新二传:

<!-- Pass on the TreeViewEx' HoverHighlighting value to each item 
    because we couldn't access it otherwise in the triggers --> 
<Setter 
    Property="HoverHighlighting" 
    Value="{Binding (Controls:TreeViewEx.HoverHighlighting), 
    RelativeSource={RelativeSource 
     AncestorType={x:Type Controls:TreeViewEx}}}" /> 

而且这是在触发部分在所有其他触发器已经:

<!-- Set the border and background when the mouse is located over 
    the item and HoverHighlighting is active --> 
<MultiTrigger> 
    <MultiTrigger.Conditions> 
    <Condition 
     Property="Controls:TreeViewExItem.HoverHighlighting" Value="True"/> 
    <Condition 
     Property="Controls:TreeViewExItem.IsMouseOver" Value="True" 
     SourceName="ElementGrid"/> 
    </MultiTrigger.Conditions> 

依赖属性和数据绑定是很大的,一旦它作品。但在那之前,这可能是可怕的。

0

我知道这是一个较旧的项目,但我想添加一些东西,我今天发现:即使您不能组合触发器和数据触发器,您可以轻松地将触发器升级到引用自我的DataTrigger,如所以:

<MultiDataTrigger.Conditions> 
    <Condition Binding="{Binding ElementName=TabsApp, Path=SelectedIndex}" value="0"/> 
    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False"/> 
</MultiDataTrigger.Conditions> 

这将使你的条件组合关于含有约其他控件,无需依赖属性条件触发控制。

相关问题