【DM部份】
为编辑介面提供Binding的数据源
protected override void DoAfterFieldChange(object sender, SmartDataColumnChangeEventArgs e) { base.DoAfterFieldChange(sender, e); string fieldName = e.Column.ColumnName; var row = this.CurrentItem.Row; if (string.Compare(fieldName, "StaffID", false) == 0) { LoadStaffOTDates(); //員工發生變化時,重新加截未沖減記錄 } } private void LoadStaffOTDates() { var row = this.CurrentItem.Row; string staffID = row.Field<Guid?>("StaffID").HasValue ? "'" + row.Field<Guid>("StaffID").ToString() + "'" : "null"; string itemID = row.Field<Guid?>("ID").HasValue ? "'" + row.Field<Guid>("ID").ToString() + "'" : "null"; //裝載員工的加班且未被沖減的記錄 var dt = AppRemoteAgent.StaticAgent.GetDataTable("select OTDate, HolidayName, RecessType from F_GetStaffOT(" + staffID + "," + itemID + ") order by OTDate"); DateTime? offsetDate = this.CurrentItem.Row.Field<DateTime?>("OffsetDate"); this.StaffOTDates = dt.DefaultView; this.CurrentItem.Row.SetField("OffsetDate", offsetDate); } #region StaffOTDates 属性 private DataView staffOTDates; public DataView StaffOTDates { get { return staffOTDates; } set { if (staffOTDates != value) { staffOTDates = value; OnPropertyChanged("StaffOTDates"); } } } #endregion【Editor窗体XMAL】
<s:EditorPage.Resources> <s:StringToDateConvert x:Key="StringToDateConvert" /> </s:EditorPage.Resources> <s:SmartLabel Caption="補假日期"> <s:SmartComboBox Text="{Binding OffsetDate, Mode=TwoWay, StringFormat=yyyy-MM-dd, Converter={StaticResource StringToDateConvert}}" ItemsSource="{Binding DataContext.StaffOTDates, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=s:EditorPage}}" IsEditable="True" IsFilteringEnabled="False" telerik:TextSearch.TextPath="OTDate"> <!-- 用Template顯示兩列 --> <s:SmartComboBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding OTDate, StringFormat=yyyy-MM-dd}" /> <TextBlock Text="{Binding HolidayName}" Grid.Column="1" /> </Grid> </DataTemplate> </s:SmartComboBox.ItemTemplate> </s:SmartComboBox> </s:SmartLabel>XAML解说:
注意:
ItemsSource中的DataContext不能漏,否则下拉就没得选了。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Data; namespace Smart.Pages { [ValueConversion(typeof(DateTime?), typeof(String))] public class StringToDateConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var date = value as DateTime?; if (date != null && date.HasValue) { return date.Value.ToString(); } return date; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string strValue = value.ToString(); DateTime resultDateTime; if (DateTime.TryParse(strValue, out resultDateTime)) { return resultDateTime; } return value; } } }
【运行效果】
具体做法参考: TK的放假登记(PubRecessEditor)