【问题标题】:WPF- trigger target not foundWPF-未找到触发目标
【发布时间】:2011-05-04 18:19:14
【问题描述】:

我在列表框上有自己的样式,我在样式数据模板和控件模板中使用。 在数据模板中,我创建了带有一些文本框的列表框项。在控件模板中,我想创建一个触发器,如果​​选择了列表框项,该触发器会更改某些文本框的前景色。

以下是一些风格:

    <Style x:Key="lbStyle" TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid Name="MainGrid">
                        <TextBlock Name="tbName" Text="{Binding Value.nick}"
                                       Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" 
                                       FontSize="13" FontWeight="Medium"></TextBlock>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="tbName" Property="Foreground" Value="Black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

问题是,我得到编译错误:找不到触发器目标 tbName。

【问题讨论】:

    标签: wpf xaml styles datatemplate controltemplate


    【解决方案1】:
    <Style TargetType="ListBox">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Margin="2" FontSize="13" FontWeight="Medium">
                        <TextBlock.Style>
                            <Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" Value="True">
                                        <Setter Property="Foreground" Value="Black"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      【解决方案2】:

      您的模板代码不正确。您将 ListBoxItem 模板应用于 ListBox 模板。此外,您没有在 ControlTemplate 中添加任何内容。

      我已经重写了:

      <Style x:Key="itemStyle" TargetType="{x:Type ListBoxItem}">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="ListBoxItem">
                          <ContentPresenter x:Name="itemContent"/>
      
                          <ControlTemplate.Triggers>
                              <Trigger Property="IsSelected" Value="true">
                                  <Setter TargetName="itemContent" Property="TextBlock.Foreground" Value="Red"/>
                              </Trigger>
                          </ControlTemplate.Triggers>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      

      应用样式的列表框:

      <ListBox ItemContainerStyle="{StaticResource itemStyle}" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多