2

我正在使用以下代码:

<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:无法使用绑定检索值,并且不存在有效的备用值;改用默认值。绑定表达式:路径=实际宽度;数据项=空;目标元素是 'Border' (Name=''); 目标属性是“宽度”(类型“双”)

可能我做的事情并不完全正确,所以您知道如何改进我的代码吗?

4

1 回答 1

2
Width="{Binding ElementName=lbItems, Path=ActualWidth}" ?
于 2010-04-16T14:34:45.130 回答