wpf binding to string
Solution 1
You cannot perform two-way binding to ObservableCollection<string>
. In order to make the strings editable you have to create a class with a string get/set property as the following class Foo
:
public class Foo
{
string _text;
public Foo(string text)
{
_text = text;
}
public string Text
{
get { return _text; }
set { _text = value; }
}
}
Your Characters
should then be of type ObservableCollection<Foo>
and your XAML should be changed so that the textboxes are binding to Foo.Text
:
<ListBox ItemsSource="{Binding Characters}" >
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding Text}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Solution 2
For those who are experiencing the exception
... binding requires path or xpath ...
You can bind the object directly this way:
<Label Content="{Binding .}" />
Solution 3
Change your TextBox binding to the following. I think it should work:
<TextBox Text="{Binding}"/>
This loads the item itself instead of a property or method output. Since the item is a string it should bind to the strings value.
Solution 4
Just remove the ToString
portion of the code.
Currently you are telling the program that you want to bind to an object called ToString
Solution 5
I take it that Characters is a public property. Debug and be sure that get is being called for Characters. If you have a the datacontext of the page/window to Movies then you need ItemsSource on the ListBox to be {Binding Path=Characters}
heyNow
Updated on June 04, 2022Comments
-
heyNow almost 2 years
I have a
Movie
class with aDim _characters = New ObservableCollection(of String)
Characters
is the associated property to get and setHow can i get characters to show up in the listBox using Binding?
So far i have the following, this isn't working as i don't know what to put instead ofToString
.<ListBox Name="cList" ItemsSource="{Binding Characters}"> <ItemsControl > <ItemsControl.ItemTemplate > <DataTemplate > <TextBox Text="{Binding ToString}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ListBox>
I want them to be editable, hence a textbox.
i tried to bind
Characters
toTextBox
directly, even that didn't work.Edit : in the code i have
parentGrid1.DataContext = me.movies
where
parent grid holds movies.-
Joachim Kerschbaumer almost 13 yearsTry just something like this : <TextBox Text="{Binding}"/> as the current context within the itemscontrol is a string. just bind to the current datacontext ({Binding}) without providing any further path.
-
heyNow almost 13 yearsgives an error saying
2 way binding requires path or xpath
-
Jakob Christensen almost 13 years@sitsOnRedChair: This is because you cannot perform two-way binding to ObservableCollection<string>. See my answer below: stackoverflow.com/questions/6943726/wpf-binding-to-string/…
-
-
heyNow almost 13 yearsYES! i just did that & it worked finally. created a class
Character
with aName
property, bound theName
to thetextBox
, its working now. -
heyNow almost 13 yearsjust out of curiosity why doesn't it work with strings? they are just like every other object
-
Jakob Christensen almost 13 yearsIt does not work with strings or any other object when you perform the binding directly to the object because the WPF binding cannot insert new objects into the ObservableCollection. It can only change properties on the objects already in the collection. If you use {Binding Text, Mode=OneWay} it will work but then of course you cannot change the strings.
-
CodeWarrior almost 13 yearsAhh I completely glossed over the fact that it was a textbox rather than a textblock. My apologies.