views:

953

answers:

3

I have overridden the windows ListBox in order to display an image and a piece of text in each ListBoxItem, but I need to filter the contents of the text displayed I was hoping to do this by accessing the DisplayMemberPath of the actual ListBox however I can't get it working.

<Setter Property="ItemContainerStyle">
        <Setter.Value>
            <!-- Simple ListBoxItem - This is used for each Item in a ListBox. The item's content is placed in the ContentPresenter -->
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid SnapsToDevicePixels="true">
                                <Border x:Name="Border">
                                    <Grid Height="40">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Image
                                            Source="{Binding Path=ThumbnailImage}"
                                            Height="30"
                                            Width="30"
                                            Grid.Column="0"/>

                                        <Label 
                                            x:Name="Text"
                                            Content="{TemplateBinding DisplayMemberPath}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            VerticalContentAlignment="Center"
                                            HorizontalAlignment="Stretch"
                                            Grid.Column="1"
                                            Height="40"/>
                                    </Grid>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="FontWeight" Value="Bold" TargetName="Text"/>
                                    <Setter Property="Foreground" Value="White" TargetName="Text"/>
                                    <Setter Property="Background" Value="Blue" TargetName="Border"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>

This is the code I am using for my style and this was the line I can't get working:

Content="{TemplateBinding DisplayMemberPath}"

It complains with: Cannot find the static member 'DisplayMemberPathProperty' on the type 'ListBoxItem'

Can anyone point me in the right direction?

A: 

Its ok, I have got the value from the ListBox now, all i need to do is convert it to take the property i need from the dataobject in the itemssource.

Just in case anyone wanted to know the code:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DisplayMemberPath}"
Bijington
A: 

Using the code above I have got the Content to display the property name or path which i then need to bind to, this is the bit I am now having problems with, does anyone know how to bind to an object using a string come back? Is this possible?

Basically i have a class:

public class A
{
    public string Name { get; set; }
    public string DateOfBirth { get; set; }
}

Now I have a collection of these classes that is populated by a db call, using the code in my previous answer I will get the content being either "Name" or "DateOfBirth" however I want it to be the value of the associated value.

Can anybody point me in the right direction?

Bijington
A: 

Did you get it working? Can you post a whole example?

I can't seem to get it working. All it shows in the listbox items is a business model object property name that i provided in the DisplayMemberPath on the ListBox eg. for: DisplayMemberPath="CategoryName" it shows CategoryName in all the listbox items.

WooYek
Hi, I am not sure I can reproduce this code anymore, I have moved on from the company where I wrote this code. I will try to rack my brains over the next few days and see what I come up with. Maybe you could post the code you are trying to use? Maybe I can see where it is you are going wrong....
Bijington