class myObject {
public List<string> GetCollection() {
return new List<string> { "AA", "BB", "CC" };
}
}
public List<string> GetCollection() {
return new List<string> { "AA", "BB", "CC" };
}
}
接著就可以在WPF視窗中加一個ObjectDataProvider ,設定要呼叫myObject的GetCollection Method,取回的集合可以直接繫結到ListBox
<Window x:Class="Demo.ODP01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ODP01" Height="300" Width="300"
xmlns:local="clr-namespace:Demo">
<Window.Resources>
<ObjectDataProvider x:Key="myObjectProvider"
ObjectType="{x:Type local:myObject}"
MethodName="GetCollection" />
</Window.Resources>
<Grid>
<ListBox
ItemsSource="{Binding Source={StaticResource myObjectProvider}}"
Height="158" HorizontalAlignment="Left" Margin="36,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="198" />
</Grid>
</Window>
執行結果<Window x:Class="Demo.ODP01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ODP01" Height="300" Width="300"
xmlns:local="clr-namespace:Demo">
<Window.Resources>
<ObjectDataProvider x:Key="myObjectProvider"
ObjectType="{x:Type local:myObject}"
MethodName="GetCollection" />
</Window.Resources>
<Grid>
<ListBox
ItemsSource="{Binding Source={StaticResource myObjectProvider}}"
Height="158" HorizontalAlignment="Left" Margin="36,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="198" />
</Grid>
</Window>
沒有留言:
張貼留言