开发者

Silverlight combobox in DataGrid

开发者 https://www.devze.com 2023-02-10 16:04 出处:网络
I have a Silverlight combo box outside of a grid which works fine. However, I cannot get it to work properly inside the data grid. I am not certain what I am doing incorrectly. Help with this is great

I have a Silverlight combo box outside of a grid which works fine. However, I cannot get it to work properly inside the data grid. I am not certain what I am doing incorrectly. Help with this is greatly appreciated! This code works fine for the silverlight combo box outside of the grid:

XAML:

  <ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=comboBoxItemDomainDataSource, Path=Data}" Margin="112,72,0,0" Name="comboBoxItemComboBox" VerticalAlignment="Top" Width="185" SelectionChanged="comboBoxItemComboBox_SelectionChanged" DisplayMemberPath="ComboDisplayValue">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
    </Grid>
    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:ComboBoxItem, CreateList=true}" Height="0" LoadedData="comboBoxItemDomainDataSource_LoadedData" Name="comboBoxItemDomainDataSource" QueryName="GetComboboxItems_PatIdEssentrisQuery" Width="0">
        <riaControls:DomainDataSource.DomainContext>
            <my:ComboBoxItemContext />
        </riaControls:DomainDataSource.DomainContext>
    </riaControls:DomainDataSource>


Combo Box Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CorporateHR.Web
{
    public class ComboBoxItem
    {
        [Key]
        public int ComboID_Int { get; set; }
        public string ComboDisplayValue { get; set; }

        private static List<ComboBoxItem> GetComboBoxItems(string strStoredProcedure)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RefConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand(strStoredProcedure, con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            List<ComboBoxItem> comboList = new List<ComboBoxItem>();
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader(behavior: CommandBehavior.CloseConnection);

            while (dr.Read())
            {
                ComboBoxItem ComboBoxItem = new ComboBoxItem();
                ComboBoxItem.ComboID_Int = Convert.ToInt32(dr[0].ToString());
                ComboBoxItem.ComboDisplayValue = dr[1].ToString();
                comboList.Add(ComboBoxItem);
            }
            return comboList;


        }


        public static List<ComboBoxItem> GetComboboxItems_PatIdEssentris()
        {
            return GetComboBoxItems("uspLookupPatIdEssentris");
        }

        //Secondary ComboBox Lookup:
        public static List<ComboBoxItem> GetComboboxItems_ORStatus()
        {
            return GetComboBoxItems("uspLookupORStatus");
        }


    }
}

Combo Box Domain Service:

namespace CorporateHR.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class ComboBoxItemService : DomainService
    {
        public IEnumerable<ComboBoxItem> GetComboboxItems_PatIdEssentris()
        {
            return ComboBoxItem.GetComboboxItems_PatIdEssentris();
        }
        public IEnumerable<ComboBoxItem> GetComboboxItems_ORStatus()
        {
            return ComboBoxItem.GetComboboxItems_ORStatus();
        }
    }

}

Code Behind Page (for combo box which populates):

  private void comboBoxItemDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void comboBoxItemComboB开发者_Go百科ox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


I used a templated column like:

<sdk:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Margin="2" VerticalAlignment="Center"  HorizontalAlignment="Left" 
                    Text="{Binding Path=Option0, Mode=OneWay}" Width="Auto" />
    </DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <ComboBox Height="23" Name="cbx0" SelectedValuePath="Display" DisplayMemberPath="Display" 
                SelectedValue="{Binding Path=Option0, Mode=TwoWay}"
                ItemsSource="{Binding Source={StaticResource DataContextProxy},Path=DataSource.ocList0}"
                        MinWidth="65"
                Width="Auto">

            </ComboBox>
        </StackPanel>
    </DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>

Where _ocList0 is of type ObservableCollection<cComboBoxOption> And this is the cComboBoxOption class:

public class cComboBoxOption
{
    public int Id { get; set; }
    public string Display { get; set; }

    public cComboBoxOption(int id, string name)
    {
        this.Id = id;
        this.Display = name;

    }

}

This was written in a generic way because I didn't know what the bindings would be or what the combo box would contain until run time.

A simpler way to do this is to use List<string> see the blog post HERE.

Silverlight combobox in DataGrid

0

精彩评论

暂无评论...
验证码 换一张
取 消