Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-未找到触发器目标_Wpf_Xaml_Styles_Datatemplate_Controltemplate - Fatal编程技术网

WPF-未找到触发器目标

WPF-未找到触发器目标,wpf,xaml,styles,datatemplate,controltemplate,Wpf,Xaml,Styles,Datatemplate,Controltemplate,我在listbox上有自己的样式,我在样式数据模板和控件模板中使用。 在数据模板中,我创建了带有一些文本框的列表框项。在控件模板中,我想创建一个触发器,如果选择了listbox项,它可以更改某些文本框的前景色 以下是一些来自style的建议: <Style x:Key="lbStyle" TargetType="{x:Type ListBox}"> <Setter Property="ItemTemplate"> <S

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

以下是一些来自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>

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


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

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