Cannot find Trigger target… the target must appear before

泄露秘密 提交于 2021-01-28 21:44:01

问题


I'm getting this warning:

Cannot find the Trigger target 'ErrandPropertiesGroupBox'. (The target must appear before any Setters, Triggers, or Conditions that use it.)

Here's the 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>

How can I fix this? (There are related posts on google but I can't piece together the right solution from them.)


回答1:


A setter inside of a DataTemplate can only reference other controls inside of that DataTemplate (that's its NameScope). So as far as I'm aware, you can't do it the way you're trying to do it.

I was trying to figure out how it could work for you, but I'm confused by your code. It looks like you're trying to set the background of a GroupBox, which is outside of an ItemsControl, whenever any item inside of a ListBox (that is itself inside of a StackPanel) is selected?

That doesn't seem to make a whole lot of sense to me. Can you clarify what you're trying to accomplish?

Edit:

Based on your comment below, I think that I would be looking towards using the ViewModel for this purpose. Binding the Visibility of the GroupBox to your ViewModel and then updating that property in your ViewModel as the selections change. Then if you wanted to make other things visible or invisible based on the same thing, you'd just have to bind to that property, not mess around with Triggers and Setters.




回答2:


From our friend the documentation:

You can set this property to the name of any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.

Your target is outside of scope, you are not supposed to change external objects from within some DataTemplate.



来源:https://stackoverflow.com/questions/6019629/cannot-find-trigger-target-the-target-must-appear-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!