I try to bind mycollection into wpf:
private ObservableCollection<MyClass> _coll;
public ObservableCollection<MyClass> Coll
{
get
{
if (_coll == null)
_coll = new ObservableCollection<MyClass>();
return _coll;
}
set
{
_coll = value;
}
}
MyClass
:
class MyClass
{
int Id;
String Name1;
String Name2;
}
And at WPF:
<ListBox Grid.Row="1" x:Name="lbKey" BorderBrush="Gray"
ItemsSource="{Binding Path=Coll}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch"
ItemContainerStyle="{StaticResource ResourceKey=lbStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="LightGray" Background="WhiteSmoke"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Margin="2"
Background="Transparent"
HorizontalAlignment="Stretch"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But, when i run program see something like Id.ToString(); So, i have to override ToString() method ,but i can not change MyClass methods.
And i create extension method:
namespace Some.Name.Space
{
public static class MyClassExtensions
{
public static string ToString(this MyClass my)
{
return String.Format("{0} {1}"my.Name1,my.Name2);
}
}
}
But this is not help to me: i see at my grid strings like this: 1,2 and so on.
Can you tell me, how to override ToString methods at extended method and bind it into WPF.