Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I work in .Net C# WPF and I want to create a ListBox with a DataTemplate to formating many data to read and write
But I wish also manipulate the elements of the ListBox independently (edit the TextBox ; position on an item)
so, I does not wish to use a Binding with an ObservableCollection

I put here a deliberately simplified example:

Xaml:
XML
<window x:class="TestListBox.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"&gt;

    <window.resources>
        <datatemplate x:key="DataTemplate1">
            <grid height="100" width="500">
                <grid.columndefinitions>
                    <columndefinition width="100" />
                    <columndefinition width="400" />
                </grid.columndefinitions>
                <grid grid.row="0">
                    &lt;Button x:Name="btnEdit" Content="Edit" ToolTip="Click to change the text"
                             Width="40"
                            Focusable="False"
                            IsTabStop="False"
                            Click="btnEdit_Click"/&gt;
                </grid>
                <grid grid.row="1">
                    <textbox>
                         Width="500" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True"
                         Text="{Binding Comment1}"
                         ScrollViewer.VerticalScrollBarVisibility="Visible"
                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"&gt;
                    </textbox>
                </grid>
            </grid>
        </datatemplate>
    </window.resources>

    <dockpanel>
        <grid dockpanel.dock="Top" height="50">
            &lt;Button Content="Add Item" Height="23" HorizontalAlignment="Left" Name="AddItem1" VerticalAlignment="Center"
			Width="75" Margin="20,0,0,0" Click="AddItem1_Click"/&gt;
        </grid>
        <scrollviewer horizontalscrollbarvisibility="Auto">
                    VerticalScrollBarVisibility="Disabled"&gt;
            <grid horizontalalignment="Left" width="500">
                <listbox x:name="ListBox1" itemtemplate="{StaticResource DataTemplate1}" itemssource="{Binding}">
                            HorizontalAlignment="Left"
				            BorderBrush="Transparent" 
				            removed="Transparent" 
                            ScrollViewer.HorizontalScrollBarVisibility="Disabled"&gt;
                </listbox>
            </grid>
        </scrollviewer>
    </dockpanel>
</window>


Code:

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

    DataContext = this;
    }

    private void AddItem1_Click(object sender, RoutedEventArgs e)
    {
        // Add Item
        ListBoxItem ListBoxItem1 = new ListBoxItem();

        // Create the ListBoxItem (which DataTemplate is DataTemplate1
        //   ???   ListBoxItem1.Comment1 = "My Text";

        ListBox1.Items.Add(ListBoxItem1);
    }

    private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        // modification of all data contained in the DataTemplate (currently I have put only comment1 for simplification)

        //   ???   Comment1 = "My new text";
    }
}



In AddItem1_Click method, I do not know how to create a ListBoxItem that uses the DataTemplate

in btnEdit_Click method, I do not know how to change the text of the textbox located in the DataTemplate

Binding done a lot of work internally and as soon as one wants to do something specific, it becomes very difficult to do.

Can you resolve my problem ?
thanks in advance
Posted
Updated 2-May-15 20:54pm
v2
Comments
Shmuel Zang 3-May-15 3:05am    
Why don't you want to use binding? MVVM is a recommended pattern. It's a good practice to change your data in your model and, just reflect the changes to the view (using the view-model).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900