在XAML中绑定列表数据模板中的项本身

4

我找不到正确的语法来直接绑定我的xaml列表中的项。

<Listview ItemsSource={Binding Items}>
    <Listview.ItemTemplate>
        <CustomUserControl Item={Binding} />
    </Listview.ItemTempalte>
</Listview>

这段代码运行良好。但是当我想要将转换器添加到绑定时,它会显示语法错误:
<Listview ItemsSource={Binding Items}>
    <Listview.ItemTemplate>
        <CustomUserControl Item={Binding ,Converter={StaticResource myConverter}} />
    </Listview.ItemTempalte>
</Listview>

有人知道如何做吗?

谢谢!


很难想象在Stack Overflow上还没有这个问题的答案... - Mong Zhu
你能否请明确你的问题? - DotNetRussell
2个回答

5
好的,所以答案就是这样。
{Binding Converter={StaticResource myConverter}}

没有逗号..


0

我认为您使用转换器的方式不正确。您可以尝试将转换器定义为静态资源。

            <converters:MyConverter x:Key="myConverter" />

并使用这种方式

<Listview ItemsSource={Binding Items}>
<Listview.ItemTemplate>
    <CustomUserControl Item="{Binding, Converter={StaticResource myConverter}" />
</Listview.ItemTempalte>

或者

<Listview ItemsSource={Binding Items}>
<Listview.ItemTemplate>
    <CustomUserControl Item="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource myConverter}" />
</Listview.ItemTempalte>


抱歉,我编辑了帖子,像你一样使用了转换器。但是,“RelativeSource={RelativeSource Self}”不起作用,因为使用此代码时,我的转换器将我的CustomeUserControl作为参数接收,而不是Items的一个项。 - Alexandre Dairay
抱歉,我误解了你的问题。也许这是答案:<CustomUserControl Item="{Binding /, Converter={StaticResource myConverter}" /> 斜杠字符 - Anton
我尝试过使用“Binding /”、“Binding Path=.”,但它总是告诉我“未找到/属性”或“未找到.属性”。 - Alexandre Dairay
而逻辑答案应该是“{Binding,Converter = {StaticResource myConverter} }”,但它无法编译。 - Alexandre Dairay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接