【问题标题】:How to define a ListBoxItem Template that has the same size of its parent?如何定义与其父级大小相同的 ListBoxItem 模板?
【发布时间】:2010-04-16 14:27:41
【问题描述】:

我正在使用以下代码:

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

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

【问题讨论】:

    标签: wpf templates listboxitem


    【解决方案1】:
    Width="{Binding ElementName=lbItems, Path=ActualWidth}" ?
    

    【讨论】:

    • 谢谢,在这种情况下它解决了问题,但在 ComboBoxItem 等默认控件上也经常发生这种情况
    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2021-01-31
    • 2021-03-13
    相关资源
    最近更新 更多