nikotromus
nikotromus

Reputation: 1064

DisplayMemberPath displaying full namespace of object

I have a listbox bound to an observable collection. This observable collection contains objects, one of which is a custom UI object that I built, which is just a button and a checkbox. I am trying to get this custom UI object show up in the listbox. I set it as the displayMemberPath, but all I get is the fully qualified namespace of the object. If I set the field ObjectName as the displayMemberPath, it works just fine. What am I doing wrong?

Here is the xaml:

<ListBox x:Name="ug_playerContainer"
     ItemsSource="{Binding ChildObjectOC}"
     DisplayMemberPath="ChildObjectControl">                                
</ListBox>

Here is the Business Object:

public class PlayerCrowdObjectBO {
    public String ObjectName { get; set; }
    public ChildObjectControl ChildObjectControl { get; set; }
}

Xaml for ChildObjectControl:

    <StackPanel
            Width="230"
            Height="35"
            Orientation="Horizontal">
            <Button
                Name="btn_childButton"
                Width="200"
                Height="25"
                Margin="5" />
            <CheckBox
                Name="cb_childCheckBox"
                Height="15"
                VerticalAlignment="Center" />
   </StackPanel>

Upvotes: 0

Views: 51

Answers (1)

Andy
Andy

Reputation: 12276

What it's saying is "what do you expect me to do with this object?"

The usual way to approach what you're doing is to bind a collection of viewmodels and get the listbox to template them into controls. Rather than bind a bunch of UI controls. If these are all of one type you could use an itemtemplate.

    <ListBox Name="ViewSelect"
             ItemsSource="{Binding ViewChoices}"
             SelectedItem="{Binding SelectedViewChoice, Mode=TwoWay}"
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                      <Button Content="{Binding Name}"
                            Command="{Binding ChoiceCommand}"
                               />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

If they're not you could define a template for each type using the DataType mechanism.

    <DataTemplate DataType="{x:Type local:Page1ViewModel}">
        <local:Page1/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:Page2ViewModel}">
        <local:Page2/>
    </DataTemplate>

Upvotes: 2

Related Questions