Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have textbox and button in the left side,i need to write new name for selected item in listbox, and after clicking on Save button my selected item's title must change.What should i write in Save button click?This is screenshot ` WPF[^]

This is my all code`

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public ObservableCollection<MyPicture> MyPictures { get; }
        = new ObservableCollection<MyPicture>();

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog op = new OpenFileDialog()
        {
            Title = "Select picture(s)",
            Filter = "All supported graphics|" +
                        "*.jpg;*.jpeg;*.png|" +
                        "JPEG (*.jpg;*.jpeg)|" +
                        "*.jpg;*.jpeg|" +
                        "Portable Network Graphic (*.png)" +
                        "|*.png",
            InitialDirectory = Environment.GetFolderPath(
                Environment.SpecialFolder.MyPictures),
            Multiselect = true
        };

        if (op.ShowDialog() == true)
        {
            paste(op.FileNames.Select(f => new MyPicture
            {
                Url = new Uri(f, UriKind.Absolute),
                Title = System.IO.Path.GetFileName(f)
            }));
        }
    }

    private void paste(IEnumerable<MyPicture> newPictures)
    {
        
        foreach (var item in newPictures)
        {
            MyPictures.Add(item);
        }
    }
}

public class MyPicture
{
    public Uri Url { get; set; } 
    public string Title { get; set; }
}


What I have tried:

I have written like this, but it's not working.

private void savebutton_Click(object sender, RoutedEventArgs e)
        {
            lb.SelectedItem= textBox.Text;
        }
Posted
Updated 3-Apr-18 2:08am
v4

Change you Listbox.ItemTemplate as follows:
XML
<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid MaxWidth="200" Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Image Source="{Binding Url}" />
            <TextBlock MouseDown="txtblk_MouseDown" Grid.Row="1"
                        Text="{Binding Title}" />
            <TextBox LostFocus="txtbox_LostFocus"  Grid.Row="1"
                        Text="{Binding Title, Mode=TwoWay}"
                        Visibility="Collapsed" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

and add the following code to your code-behind:
C#
private void txtblk_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    TextBox txt = (TextBox)((Grid)((TextBlock)sender).Parent).Children[2];
    txt.Visibility = Visibility.Visible;
    ((TextBlock)sender).Visibility = Visibility.Collapsed;
}

private void txtbox_LostFocus(object sender, RoutedEventArgs e)
{
    TextBlock tb = (TextBlock)((Grid)((TextBox)sender).Parent).Children[1];
    tb.Visibility = Visibility.Visible;
    ((TextBox)sender).Visibility = Visibility.Collapsed;
}

For the changed to be reflected using data binding, you need to change the MyPicture class as follows:
C#
public class MyPicture : ObservableBase
{
    public Uri Url { get; set; } // filename of image

    private string title;
    public string Title
    {
        get { return title; }
        set { Set(ref title, value); }
    }
}

and add the following class to support the INotifyPropertyChanged interface:
C#
public abstract class ObservableBase : INotifyPropertyChanged
{
    public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
    {
        if (EqualityComparer<TValue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
        {
            field = newValue;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

The INotifyPropertyChanged interface notifies the data binding system there is a change when the PropertyChanged event is raised. I've wrapped this up in a base class making the usage very simple. You can see this in the Title property Set method of the MyPicture class.

To use, click on the Picture's title text and you will be able to edit it. Once you click outside the editing TextBox, the changes will automatically be applied.
 
Share this answer
 
Comments
Suren97 3-Apr-18 8:28am    
Thank you very much :) but it just very hard for me,i wanted more simple option, just to write name in textbox, then after clicking on button save,it will be changed.But nothing it's work Great,Thank you so much :)
Graeme_Grant 3-Apr-18 8:43am    
It's in-place editing, just like Windows Explorer... Pete's version will also do what you want.
Suren97 4-Apr-18 9:11am    
Hi, I have a new question, can you help me please?This is my question
C# how to save datas in list, then add them in listview (WPF)[^]
Graeme_Grant 4-Apr-18 10:34am    
I posted a reply for you, however, there is no code in my answer. Instead, I have given you two links that will better answer (than I can in the context of this site) your question. They will also answer a number of future questions that you will have with the ListView control, the same ones that I had when I was, and still am, using it.
SelectedItem returns an object, not an instance of MyPicture. What you need to do is convert the selected object into MyPicture and then assign the text to the value. Something like this:
C#
private void UpdateTitle()
{
  if (lb.SelectedItem == null) return;
  MyPicture picture = lb.SelectedItem as MyPicture;
  if (picture != null)
  {
    picture.Title = textBox.Text;
  }
}
 
Share this answer
 
Comments
Suren97 3-Apr-18 6:51am    
I tried like this, but it's not change again :(
Pete O'Hanlon 3-Apr-18 6:54am    
Did you put any breakpoints in your code and debug it to see what's happening?
Suren97 3-Apr-18 6:57am    
which breakpoint?
Pete O'Hanlon 3-Apr-18 7:03am    
In your code, did you add a breakpoint on the point that you are testing that SelectedItem is not null and then step over each line, looking to see what the values are? You do know how to debug don't you?
Suren97 3-Apr-18 7:15am    
yes i added, but it's not change selected item's title

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