【问题标题】:WPF The property 'Content' cannot be data bound to a visual elementWPF 属性“内容”不能是绑定到可视元素的数据
【发布时间】:2013-05-04 23:19:58
【问题描述】:

我有一个列表视图,在第一列我尝试绑定基础集合的索引。

<Window.Resources>
    <local:IndexConverter x:Key="IndexConverter" />
</Window.Resources>
....
<ListView>
    <ListView.View>
        <GridView>          
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Label Width="70" Height="40" 
Content="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Converter={StaticResource IndexConverter}}"></Label>
                        <!-- More stuff -->
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

// IndexConverter
public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
{
     // Get row number. This part is working.
     // Return the formatted row number
     return string.Format("#{0}", rowNumber);
}

当我编译这个时,我得到以下错误:

“内容”属性不能将数据绑定到可视元素。

绑定有什么问题?

【问题讨论】:

    标签: wpf listview binding


    【解决方案1】:

    我认为您正在尝试将列表视图项(我猜这是一个视觉元素)直接绑定到您的内容中:我看不到您的绑定路径,只有源。

    尝试将某些内容绑定到您的内容

    <Label Width="70" Height="40"  Content="{Binding Path=SomeThing,RelativeSource=RelativeSource AncestorType=ListViewItem},Converter={StaticResource IndexConverter}}">
    

    【讨论】:

    • 我应该设置什么作为路径?我想要的总结:我得到选定的列表视图项,将其传递给转换器,然后将值写入内容字段。
    • 通过列表视图项目,您的意思是作为可视组件的项目或背后的业务对象(即可能作为 ItemSource 绑定到列表视图的列表对象之一)??
    猜你喜欢
    • 1970-01-01
    • 2010-09-30
    • 2018-05-14
    • 2015-08-30
    • 2013-02-18
    • 2011-01-27
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多