Search code examples
c#wpfwindows-phone-8listboxpivotitem

In PivotItem, ListBox Binding dont show value


Im create collection for show in ListBoxTransactions and binding as Description. But in result i have only name collection in ListBoxTransactions.ItemsSource, but not value. adn a cant use ListBox.ItemTemplate

XAML

<phone:PivotItem Header="Journal">
      <Grid>
         <ListBox Name="ListBoxTransactions">
            <StackPanel Orientation="Horizontal">
               <TextBlock Text="{Binding Description}" FontSize="35"/>                            
            </StackPanel>
         </ListBox>
      </Grid>
</phone:PivotItem>

C#

public class TransactHelper
    {
        public string Description { get; set; }        
    }


public void ShowTransactions()
        {
            ListBoxTransactions.Items.Clear();
            var transactFulls = _workerDb.GeTransactFull();
            var list = new List<TransactHelper>();
            foreach (var t in transactFulls)
            {                
                list.Add(new TransactHelper { Description = t.Description });
            }

            this.ListBoxTransactions.ItemsSource = list; // dont view collection. only name collection

Solution

  • You should implement ItemContainerStyle for your items.

    <ListBox Name="ListBoxTransactions" ItemContainerStyle="{DynamicResource MyItemStyle}">
    <ListBox.Resources>
        <Style x:Key="MyItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                     <ControlTemplate>
                         <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Description}" FontSize="35"/>                            
                          </StackPanel>
                     </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
    </ListBox>
    

    And once note: Don't use this pair

    ListBoxTransactions.Items.Clear();
    this.ListBoxTransactions.ItemsSource = list;
    

    You need

    ListBoxTransactions.ItemsSource = null;
    this.ListBoxTransactions.ItemsSource = list;
    

    Or Implement your collection as ObservableCollection