WPF-未找到触发目标

发布于 2024-09-30 16:13:01 字数 1390 浏览 1 评论 0原文

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

以下是一些风格:

    <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。

I have own style on listbox, I use in style data template and also control template.
In data template I create listbox item with some textboxes. In control template I want create a trigger which change foreground color of some textbox if listbox item is selected.

Here is some from style:

    <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>

Problem is, I get compile error : Cannot find the Trigger target tbName.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-10-07 16:13:01
<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>
<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>
血之狂魔 2024-10-07 16:13:01

您的模板代码不正确。您将 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:

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

Your code of template is incorrect. You apply ListBoxItem template to ListBox template. Also, you didn't add anything inside ControlTemplate.

I have rewrited it:

<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 with applied style:

<ListBox ItemContainerStyle="{StaticResource itemStyle}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文