代码之家  ›  专栏  ›  技术社区  ›  kime waza

从列表框项中删除焦点矩形

  •  2
  • kime waza  · 技术社区  · 16 年前

    <ListBox x:Name="MyListBox" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid >
                   ...snipped...
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
    

    当我运行它时,我得到一个异常

    System.Windows.Markup.XamlParseException: Invalid attribute value FocusVisualStyle for property Property. [Line: 47 Position: 38]
    

    我做错什么了?非常感谢:)

    1 回复  |  直到 16 年前
        1
  •  1
  •   AnthonyWJones    16 年前

    在银色的灯光下 ListBoxItem FocusVisualStyle 属性的错误。

    为了实现你的目标,你需要提供一个新的模板 ListBoxItem项目 . 从Silverlight文档中可以找到默认模板 ListBox Styles and Templates

    将ListBoxItem模板复制到静态资源中(App.Xaml将是一个好地方)

    <ControlTemplate TargetType="ListBoxItem" x:Key="ListBoxItemSansFocus">
     <!-- copy of the rest of the control template here -->
    </ControlTemplate>
    

    现在拆下 StoryBoard VisualState 并移除名为“FocusVisualElement”的最后一个矩形。

    ContainerStyle 物业外观like:-

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Template" Value="{StaticResource ListBoxItemSansFocus}" />
        </Style>
    </ListBox.ItemContainerStyle>
    
    推荐文章