MainWindow.xaml文件
<Window x:Class="WpfApp20140821.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp20140821" xmlns:system="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525" x:Name="hostCtrl"> <Grid> <TabControl> <TabItem Header="tabItem1"> <DataGrid x:Name="dataGrid1" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Width="40" Header="案例编号" Binding="{Binding Path=Id}"/> <DataGridTextColumn Width="100" Header="案卷编号" Binding="{Binding Path=RollID}"/> <DataGridComboBoxColumn Width="60" Header="行政区划" ItemsSource="{x:Static local:MainWindow.SelectionList}"> <!--<DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=this}"/> <Setter Property="SelectedValue" Value="{Binding Path=SelectedValue}"/> </Style> </DataGridComboBoxColumn.ElementStyle>--> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> </TabItem> <TabItem Header="tabItem2" HorizontalAlignment="Right"> </TabItem> </TabControl> </Grid> <Window.Resources> <!--<ObjectDataProvider x:Key="SelectionList" MethodName="GetValues" ObjectType="{x:Type local:MainWindow}"> <ObjectDataProvider.MethodParameters> <system:String>string1</system:String> <system:String>string2</system:String> </ObjectDataProvider.MethodParameters> </ObjectDataProvider>--> </Window.Resources> </Window>
MainWindow.xaml.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; using System.Collections.ObjectModel; namespace WpfApp20140821 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { static ObservableCollection<string> selectionList = new ObservableCollection<string>(); public static ObservableCollection<string> SelectionList { get { return selectionList; } set { selectionList = value; } } private string GetValues(string str1,string str2) { return str1 + str2; } public MainWindow() { InitializeComponent(); string strSql = "select * from SP_CaseInfoMGT"; DataTable dt = SqlHelp.QueryDT(strSql); this.dataGrid1.ItemsSource = dt.AsDataView(); for (int i = 0; i < dt.Rows.Count; i++) { selectionList.Add(dt.Rows[i]["District"].ToString()); } } } }
存在问题:ComboBox的SelectedValue的设置问题
转载:http://blog.csdn.net/foreverling/article/details/38759957