【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解说:
1.StringFormat=yyyy-MM-dd, 解决 ComboBox显示日期字段的格式问题;
2.IsEditable="True" 和 IsFilteringEnabled="False", 允许手工输入日期;
3.StringToDateConvert转换器, 解决字符串转日期问题;
4.<s:SmartComboBox.ItemTemplate>...</s:SmartComboBox.ItemTemplate>显示日期+假期名称(多栏)问题;
5.telerik:TextSearch.TextPath="OTDate"解决选中记录赋值问题;
注意:
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)