2010-04-16 43 views
2

我使用下面的代碼:如何定義一個ListBoxItem模板,它的父級大小相同?

<ListBox 
    x:Name="lbItems"    
    Grid.Row="1" 
    Margin="2"> 
    <ListBox.Template> 
     <ControlTemplate> 
      <Border 
       Background="{StaticResource DarkerBrush}"        
       Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, Mode=FindAncestor}}" 
       BorderBrush="{StaticResource MediumBrush}" 
       BorderThickness="0" 
       CornerRadius="4"> 
       <ItemsPresenter />        
      </Border> 
     </ControlTemplate> 
    </ListBox.Template> 
</ListBox> 

它的工作原理,但我看到的,我想刪除輸出窗口例外。

System.Windows.Data信息:10:使用 約束力和沒有有效的回退值 存在 無法檢索值;改爲使用默認值。 BindingExpression:Path = ActualWidth; DataItem = null;目標元素是 'Border'(Name ='');目標屬性 「寬度」(類型「雙」)

也許我做的東西不完全正確的,那麼你有什麼想法,我如何提高我的代碼

回答

2
Width="{Binding ElementName=lbItems, Path=ActualWidth}" ? 
+0

謝謝你,在這種情況下,它解決了這個問題,但它也經常發生在像ComboBoxItem這樣的默認控件上 – Drake 2010-04-19 09:03:22

相關問題