DataTemplate with List binding
-
Hi, I managed to bring to work simple data template in a ListView control:
<DataTemplate>
<TextBox Text="{Binding Path=boo}" />
</DataTemplate>Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have
List<int> integers;
. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like<TextBox Text="{Binding Path=integers}" />
by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul
-
Hi, I managed to bring to work simple data template in a ListView control:
<DataTemplate>
<TextBox Text="{Binding Path=boo}" />
</DataTemplate>Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have
List<int> integers;
. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like<TextBox Text="{Binding Path=integers}" />
by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul
One way to do this would be to use a ViewModel approach, and bind to a string property that aggregates this data. Consider the following sample:
public class MyClass
{
private List<int> _myList = new List<int>();
public string AggregateList
{
get
{
if (_myList != null && _myList.Count > 0)
{
StringBuilder myString = new StringBuilder();
foreach (int value in _myList)
{
sb.AppendFormat("{0},", value);
}
return sb.ToString().Substring(0, sb.ToString().Length - 2);
}
return string.Empty;
}
set
{
string[] values = value.Split(",");
_myList.Clear();
foreach (string item in values)
{
int output;
if (int.TryParse(item, out output))
{
_myList.Add(output);
}
}
}
}
}You could do something like this using converters, but this is a simple method - and you just need to bind to this property. I've just knocked this up in the HTML editor here, so there may be a mistake or two, so I apologise in advance for them.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Hi, I managed to bring to work simple data template in a ListView control:
<DataTemplate>
<TextBox Text="{Binding Path=boo}" />
</DataTemplate>Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have
List<int> integers;
. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like<TextBox Text="{Binding Path=integers}" />
by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul
-
One way to do this would be to use a ViewModel approach, and bind to a string property that aggregates this data. Consider the following sample:
public class MyClass
{
private List<int> _myList = new List<int>();
public string AggregateList
{
get
{
if (_myList != null && _myList.Count > 0)
{
StringBuilder myString = new StringBuilder();
foreach (int value in _myList)
{
sb.AppendFormat("{0},", value);
}
return sb.ToString().Substring(0, sb.ToString().Length - 2);
}
return string.Empty;
}
set
{
string[] values = value.Split(",");
_myList.Clear();
foreach (string item in values)
{
int output;
if (int.TryParse(item, out output))
{
_myList.Add(output);
}
}
}
}
}You could do something like this using converters, but this is a simple method - and you just need to bind to this property. I've just knocked this up in the HTML editor here, so there may be a mistake or two, so I apologise in advance for them.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Thank you for your time! This should be enough in some cases, including my current issue, but it seems somewhat less flexible. Let me be a bit curious, so what if an user wants to write his own style(?) and have the data separated with semicolons, not commas? May seem overcomplicated, but I'm asking, because it may happen that the end-user will be unable to change the data input. So the List<int> will be always a List<int> without a chance to wrap it as above. Thanks once again! Anyway I've just found a site about converter binding in xaml and I think it can be highly educational.
-
You could put the textbox in a stackpanel and then set the stackpanel's orientation to horizontal.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for... -
<ListBox x:Name="DemoList"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition> <ColumnDefinition> </Grid.ColumnDefinitions> </Grid> <TextBox grid.column=0 Text1="{Binding integers}"/> <TextBox grid.column=1 Text1=";"/> </DataTemplate> </ListBox>
You might also want to take a look here[^]. -
Thank you for your time! This should be enough in some cases, including my current issue, but it seems somewhat less flexible. Let me be a bit curious, so what if an user wants to write his own style(?) and have the data separated with semicolons, not commas? May seem overcomplicated, but I'm asking, because it may happen that the end-user will be unable to change the data input. So the List<int> will be always a List<int> without a chance to wrap it as above. Thanks once again! Anyway I've just found a site about converter binding in xaml and I think it can be highly educational.
You could use a converter if you wanted - if the user wants to write his own style though, you are faced with having to change the code anyway. The sample I posted was aimed at satisfying your problem - it's designed around POCO (Plain Old CLR Objects) which means that it's easy to test.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You could put the textbox in a stackpanel and then set the stackpanel's orientation to horizontal.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for...The original question was how could you wrap the list into one textbox, not how to display the list horizontally.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.