在WPF中选择时更改列表框项样式

时间:2018-03-27 19:20:46

标签: c# wpf

我不完全确定我做错了什么,但似乎我的风格触发器未被识别。我想在选择列表框项目时更改笔划的颜色。

<ListBox ItemsSource="{Binding CityList}" DisplayMemberPath="Name" SelectionMode="Extended"
                 VirtualizingPanel.IsVirtualizing="true"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 Background="Brown">

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Canvas.Left" Value="{Binding Longitude, Converter={StaticResource longValueConverter}, ConverterParameter={StaticResource mapWidth}}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Latitude, Converter={StaticResource latValueConverter}, ConverterParameter={StaticResource mapHeight}}"/>
                    <Setter Property="BorderThickness" Value="3" />

                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <Grid>
                                    <Ellipse x:Name="indicator" 
                                             Fill="#FF000000" 
                                             Height="10" 
                                             Width="10" 
                                             Stroke="Transparent"
                                             StrokeThickness="2"/>
                                </Grid>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="indicator" Property="Stroke" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas IsItemsHost="True"
                            Width="{StaticResource mapWidth}"
                            Height="{StaticResource mapHeight}"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

        </ListBox>

1 个答案:

答案 0 :(得分:2)

您无法在样式设定器中使用TargetName

您可以设置Template属性并将Trigger添加到ControlTemplate.Triggers集合,而不是设置ContentTemplate属性:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Grid>
                <Ellipse x:Name="indicator" 
                         Fill="#FF000000" 
                         Height="10" 
                         Width="10" 
                         Stroke="Transparent"
                         StrokeThickness="2"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="indicator" Property="Stroke" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

您还可以在ControlTemplate中向Grid添加一个ContentPresenter,它将显示ItemTemplate的元素(如果您以后决定声明一个)。