2010-02-24 45 views
2

如何從Silverlight列表框中移除焦點矩形?我有這樣的代碼:從列表框中移除焦點矩形項目

<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

在Silverlight中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> 

現在請從「重點」 VisualStateStoryBoard並刪除了名爲「最後的矩形FocusVisualElement」。

現在使你的財產ContainerStyle樣子: -

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