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
; }
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.
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:
private
void
UpdateTitle()
if
(lb.SelectedItem ==
null
)
return
;
MyPicture picture = lb.SelectedItem
as
MyPicture;
if
(picture !=
null
)
picture.Title = textBox.Text;
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.