找不到触发器目标......目标必须出现在之前

时间:2011-05-16 15:26:10

标签: wpf triggers datatemplate

我收到了这个警告:

找不到触发器目标“ErrandPropertiesGroupBox”。 (目标必须出现在任何使用它的Setter,Triggers或Conditions之前。)

这是XAML:

<UserControl.Resources>
    <ResourceDictionary>
        <ObjectDataProvider x:Key="ErrandData" />

        <DataTemplate x:Key="GroupTemplate">
            <GroupBox>
                <GroupBox.Header>
                    <WrapPanel>
                        <Label Content="Group #" />
                        <Label Content="{Binding Path=df_groupOrder}" />
                    </WrapPanel>
                </GroupBox.Header>
                <ListBox ItemsSource="{Binding Path=df_errands}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Label Name="label1" Content="{Binding Path=TypeName}" />
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
                                    <Setter TargetName="ErrandPropertiesGroupBox" Property="Background" Value="HotPink" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </GroupBox>
        </DataTemplate>

    </ResourceDictionary>
</UserControl.Resources>

<WrapPanel Name="rootWrapPanel">
    <ItemsControl ItemsSource="{Binding Source={StaticResource ErrandData}, Path=df_sequentialErrandGroup}" ItemTemplate="{StaticResource GroupTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    <GroupBox Name="ErrandPropertiesGroupBox" Header="Errand Properties" />
</WrapPanel>

我该如何解决这个问题? (谷歌上有相关的帖子,但我无法拼凑出正确的解决方案。)

2 个答案:

答案 0 :(得分:2)

DataTemplate内的setter只能引用DataTemplate内的其他控件(即NameScope)。所以据我所知,你不能像你想要的那样去做。

我试图找出 为你工作的方式,但我对你的代码感到困惑。看起来你试图设置GroupBox的背景,它位于ItemsControl之外,只要ListBox内的任何项目(它本身位于StackPanel内1}})被选中了?

这对我来说似乎没有多大意义。你能澄清一下你想要完成的事情吗?

修改

根据您在下面的评论,我认为我会考虑将ViewModel用于此目的。将Visibility的{​​{1}}绑定到GroupBox,然后随着选择的更改更新ViewModel中的该属性。然后,如果你想根据相同的事情使其他事物可见或不可见,你只需要绑定到该属性,而不是混淆ViewModelTriggers

答案 1 :(得分:0)

来自我们的朋友documentation

  

您可以将此属性设置为应用setter集合(此setter所属的集合)范围内的任何元素的名称。这通常是包含此setter的模板中的命名元素。

您的目标超出了范围,您不应该在某些DataTemplate中更改外部对象。