0

Normally I bind ObservableCollection to my own classes. But in this case I need to bind ObservableCollection of Strings in ListBox using MVVM into WPF.

My xml is

<Window x:Class="ListBoxDynamic.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:vm="clr-namespace:ListBoxDynamic.ViewModel">
<Grid Margin="0,0,-8,0">
    <ListBox Width="100" Height="90"  ItemsSource="{Binding ListOfItems}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton Command="{Binding SelectItemCommand}" CommandParameter="{Binding ElementName=Item}" >
                    <ToggleButton.Template>
                        <ControlTemplate TargetType="ToggleButton">
                            <TextBlock x:Name="Item" Text="{Binding What I must to write?}" />
                        </ControlTemplate>
                    </ToggleButton.Template>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

My MainViewModel

private ObservableCollection<string> _listOfItems = new ObservableCollection<string>();
    public ObservableCollection<string> ListOfItems
    {
        get { return _listOfItems; }
        set { _listOfItems = value; RaisePropertyChanged("ListOfItems"); }
    }

    public ICommand SelectItemCommand { get; private set; }
    int counter = 1;
    public MainViewModel()
    {
        ListOfItems.Add("Add new Item");
        SelectItemCommand = new RelayCommand<object>((x) => ExecuteSelectItemCommand(x));

    }

But I don't know that I must to write in Binding TextBlock into ToggleButton.

9
  • Wrap the string as a property of a class? Commented Jun 12, 2014 at 3:29
  • I don't want do it, Isn't possible only string? I don't want create a new class only for binding a property. Commented Jun 12, 2014 at 3:32
  • Just write <TextBlock x:Name="Item" Text="{Binding }" /> Commented Jun 12, 2014 at 3:38
  • I tried this nit , but ("Add New Item") element, it doesn't appear. Commented Jun 12, 2014 at 3:41
  • 1
    this is the most common mistake that has made every wpf developer want to kick himself Commented Jun 12, 2014 at 4:17

2 Answers 2

2

Since data which back each ListBoxItem is string, hence DataContext of each ListBoxItem will be string.

Hence doing <TextBlock x:Name="Item" Text="{Binding }" /> will show you the string backing the listboxitem in the textblock

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, @nit i have some similar issue seeking some assistance
where?, here it self?
1

there are few issues I notices

  • the command is available in the view model instead of the data item, so use relative source to bind to command
  • "{Binding}" can be used to refer to current Item, so no need to use elementname syntax
  • then instead of placing a textbox inside toggle button you can make use of content presenter to make it more flexible

so this could go like this

<ListBox Width="100"
         Height="90"
         ItemsSource="{Binding ListOfItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton Command="{Binding SelectItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"
                          CommandParameter="{Binding}"
                          Content="{Binding}">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="ToggleButton">
                        <ContentPresenter />
                    </ControlTemplate>
                </ToggleButton.Template>
            </ToggleButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

3 Comments

But... The first element that I added, does not appear when I run my app.
that may not be the issue with the template, try to check the collection if there is the value, also see if there is any binding error in output window.
Great find @ncampuzano, hoping this template work for you, this is flexible enough to accommodate any type of content providing a toggle behavior, eg image button, drawing button etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.