开发者

Silverlight: How to use a converter with an ItemsControl?

I have a开发者_开发技巧n ItemsControl whose ItemsSource is bound to a list of ints IDs. A converter uses the IDs to look up the name that should be displayed to the user. How can I do this in XAML? Here is what I have so far, but it doesn't work:

    <ItemsControl ItemsSource="{Binding Topics}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FallbackValue='topic name', Converter={StaticResource topicToStrConverter}}" Margin="0,10,0,0"/>
                    <Button>
                        <Image Source="/PlumPudding;component/Images/appbar.cancel.rest.png" />
                    </Button>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Really, what I want as an argument to the converter is the entire item that is being displayed in the template - not a property of that item. What is the syntax for this?

I'm using Silverlight 4.


if Topics is List, then what you have is correct.

However, if Topics is List, and Id is a property of the Topic class, you will need to use "Path=Id". So: {Binding FallbakcValue='Bla', Path=Id, Converter={StaticResource yourConverter}

To answer you second question: "what I want as an argument to the converter is the entire item that is being displayed in the template - not a property of that item"

This syntax you are using will pass the entire object in the List, so in your case an int is passed to the converter. Again, if it is a list, then the Topic object is passed to the converter.


Your code is right to my opinion..

It seems that problem in 'converter'. Try to debug code of topicToStrConverter.


You have to set the DataContext for the items control or for one of it's parents. If you don't do this there is no context for the binding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜