用ComBoBox选择冲减日期

【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不能漏,否则下拉就没得选了。

【StringToDateConvert转换器, 解决字符串转日期问题】
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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值