【问题标题】:WPF Data Binding Error in ListBox列表框中的 WPF 数据绑定错误
【发布时间】:2010-12-15 03:56:33
【问题描述】:

我有一个ListBox

<ListBox x:Name="HistogramListBox" Grid.Column="1" Margin="8,2,8,0"
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
         Template="{StaticResource HistogramListBoxControlTemplate}"
         ItemContainerStyle="{StaticResource HistogramListBoxItem}"
         ItemTemplate="{DynamicResource BucketTemplate}" />

使用DataTemplate 反过来又使用ValueConverter 来确定@​​987654325@ 的高度:

<DataTemplate x:Key="BucketTemplate">
    <StackPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                <Rectangle Grid.Row="0" StrokeThickness="1" VerticalAlignment="Bottom" 
                           Stroke="{Binding ElementName=MainElement, Path=BucketStroke}" 
                           Fill="{Binding ElementName=MainElement, Path=BucketFill}" >
                    <Rectangle.Height>
                        <MultiBinding Converter="{StaticResource HistogramValueToPercentageConverter}">
                            <Binding Mode="OneWay" Path="ItemCount" />
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:Histogram}}" />
                        </MultiBinding>
                    </Rectangle.Height>
                </Rectangle>
            </StackPanel>
        </Grid>
    </StackPanel>
</DataTemplate>

ListBoxItemsSource 是一个int[]

当我执行代码时,它说在 Int32 上找不到“ItemCount”。我以为它从ListBox 得到了项目计数(我显然错了)。

谁能告诉我如何让我的ValueConverter 知道我在做什么。

谢谢

【问题讨论】:

    标签: wpf data-binding binding listbox ivalueconverter


    【解决方案1】:

    数据模板中项目的数据上下文是数据项本身,即int。如果您想要ListBox 上的属性,您需要在当前上下文之外进行操作。您可以使用RelativeSource 来执行此操作:

    {Binding Items.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}
    

    【讨论】:

    • 我很确定 ListBox 没有 ItemCount 属性,对吧?
    • @Drew:是的。我刚刚复制了OP。更新了我的帖子。
    • Items.Count 给了我项目的总数。我正在寻找它当前在列表框中的项目。谢谢
    【解决方案2】:

    假设您的第一个转换器参数是绘制的实际值,第二个是直方图对象:

    <Rectangle.Height>
      <MultiBinding Converter="{StaticResource HistogramValueToPercentageConverter}">
        <Binding />
        <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:Histogram}}" />
      </MultiBinding>
    </Rectangle.Height>
    

    这是因为 DataContext 本身就是整数,至少从您给出的错误消息看来是这样。

    顺便说一句,您通常会使用绑定来设置 ListBox 的 ItemsSource,而不是通过代码隐藏。这导致 UI 和代码的分离更加清晰。我注意到您的示例代码中没有显示ItemsSource=,所以我想我应该提到这一点。

    【讨论】:

      【解决方案3】:

      你可以试试这个为你的绑定:

      <Binding Path="Items.Count">
          <Binding.RelativeSource>
              <RelativeSource AncestorType="{x:Type ListBox}" />
          </Binding.RelativeSource>
      </Binding>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 2020-02-14
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多