Microsoft Developer Network >
Página Inicial dos Fóruns
>
Windows Presentation Foundation (WPF)
>
ComboBox Data Binding in XAML
ComboBox Data Binding in XAML
Hi
The ComboBox data binding is on a DataTable which is returned from a method call on a singleton instance object.
Here is my XAML<Window x:Class="MainWindow" xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:data="clr-namespace:System.Data;assembly=System.Data" Title="Database" Height="495" Width="420" ResizeMode="CanResizeWithGrip"> <Window.Resources> <ObjectDataProvider x:Key="DataTable" ObjectType="{x:Type data:Sql.SqlDataSourceEnumerator}"/> </Window.Resources> <StackPanel> <ComboBox Name="comboBoxServer" ItemsSource="{Binding Source= {x:Static data:Sql.SqlDataSourceEnumerator.Instance}}" DisplayMemberPath="ServerName" /> </StackPanel> </Window>
The method I want to call is GetDataSources(), for example in code it would appears:
System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
So the question is, how do I declarative make this call using XAML binding the return DataTable to a ComboBox?
Craig
Respostas
- Solution
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:data="clr-namespace:System.Data;assembly=System.Data" Title="Database" Height="495" Width="420" ResizeMode="CanResizeWithGrip"> <Window.Resources> <ObjectDataProvider x:Key="DataTable" ObjectInstance="{x:Static data:Sql.SqlDataSourceEnumerator.Instance}" MethodName="GetDataSources" IsAsynchronous="True"/> </Window.Resources> <StackPanel> <ComboBox ItemsSource="{Binding Source={StaticResource DataTable}, Mode=OneWay}" DisplayNamePath="ServerName"/> </StackPanel> </Window> - Marcado como RespostaForum Craig terça-feira, 10 de fevereiro de 2009 7:06
Todas as Respostas
- Sugerido como RespostaIrfanAhmad terça-feira, 10 de fevereiro de 2009 9:39
- Solution
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:data="clr-namespace:System.Data;assembly=System.Data" Title="Database" Height="495" Width="420" ResizeMode="CanResizeWithGrip"> <Window.Resources> <ObjectDataProvider x:Key="DataTable" ObjectInstance="{x:Static data:Sql.SqlDataSourceEnumerator.Instance}" MethodName="GetDataSources" IsAsynchronous="True"/> </Window.Resources> <StackPanel> <ComboBox ItemsSource="{Binding Source={StaticResource DataTable}, Mode=OneWay}" DisplayNamePath="ServerName"/> </StackPanel> </Window> - Marcado como RespostaForum Craig terça-feira, 10 de fevereiro de 2009 7:06
- Yes, that is what explained in the article I had pointed in my last post :-(
Irfan Ahmad - This works great, but being new to WPF and how to bind, I am wondering if there is a way to know when this process is complete.
Basically, I want to know if there is a way (BackgroundWorker, for example) to know when the asynchronous method is complete so I can enable other UI controls.
How would I be able to use XAML to tie WorkerSupportsCancellation="True" and cancel this request?
Also, what if I wanted the data to go directly to a model, wait for completion, and then update the combobox?

