Maverick
Maverick

Reputation: 2032

bind the data to list picker using code behind

i have a user control, in that i am having one list picker. code is :

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>

        <toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" />
        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" >
            <Button Width="200" x:Name="btnReAssign" Content="reassign" Margin="5"/>
            <Button Width="200" x:Name="btnCancel" Content="cancel"  Margin="5"/>
        </StackPanel>        
</Grid>

Now i m creating the pop up user control at run time.

MyTaskPopupWindow myTaskPopUpWindow = new MyTaskPopupWindow();

this contains the list picker. Now i am binding this listpicker with my data object.

 myTaskPopUpWindow.lstPicker.ItemsSource = GetRegisterUserOC;

but class name is displaying in the list picker, not the property. i am not getting how should i bind the one of the property to this list picker. Can i bind one of the property from code behind, as i dont want to make changes in user control.

Upvotes: 0

Views: 441

Answers (2)

Loetn
Loetn

Reputation: 4040

I never used ListPicker, but can't you set the DisplayMemberPath to the property you want to show (eg. Name)?

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="tbkTitle" Margin="25,25,25,15" FontSize="32" />
        <toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" DisplayMemberPath="Property" />
        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" >
            <Button Width="200" x:Name="btnReAssign" Content="reassign" Margin="5"/>
            <Button Width="200" x:Name="btnCancel" Content="cancel"  Margin="5"/>
        </StackPanel>        
</Grid>

Or

 myTaskPopUpWindow.lstPicker.DisplayMemberPath = PropertyName;

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26078

Typically you do something like this:

<toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" >
    <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding YourDisplayPropertyOnObject}"/>
        </DataTemplate>
    </toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>

If you would rather take a lazier approach, you can simply override the ToString() property on the object you are binding to display how you would like.

Upvotes: 1

Related Questions