添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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; < /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:
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:
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:
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.
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;
Did you step over each line and check what it was doing? Did you ensure that the conversion of SelectedItem to MyPicture actually worked?

Oh, one more thing, if you are expecting to see the value updated back in the UI, you are going to have to raise a PropertyChanged event in the Title when you change it.
Seriously? You're doing WPF and you don't know about PropertyChanged? Your MyPicture class needs to implement INotifyPropertyChanged; this has a PropertyChanged event. In the setter for your title, you are going to have to raise the event. Have a search around the web for ViewModelBase implementations to get an idea about how to structure this.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •