提出问题提出问题
 

已答复ComboBox Data Binding in XAML

  • 2009年2月10日 3:22Forum Craig 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码

    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

答案

  • 2009年2月10日 7:05Forum Craig 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    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> 

全部回复

  • 2009年2月10日 6:58IrfanAhmad 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复
     Read the following article;

    http://msdn.microsoft.com/en-us/library/aa348824.aspx
    Irfan Ahmad
  • 2009年2月10日 7:05Forum Craig 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    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> 
  • 2009年2月10日 9:39IrfanAhmad 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Yes, that is what explained in the article I had pointed in my last post :-(
    Irfan Ahmad
  • 2009年7月4日 22:34Investor5555 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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?