置百丈玄冰而崩裂,掷须臾池水而漂摇。

WPF DataGrid 多绑定设置

ASP.NET 强强

WPF  DataGrid 多绑定设置


在WPF中 DataGrid 转换使用多绑定参数转换的使用方法:

1.集成 IMultiValueConverter 接口,实现一个转换器:

namespace XQ.View
{
    public class AgeTransformation : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            string agestr = string.Empty;
            try
            {
                var patage = values[0].ToString();
                var birdate = values[1].ToString();
                if ((patage.IsNullOrWhiteSpace() &&birdate.IsNullOrWhiteSpace()))
                {
                    return string.Empty;
                }
                if (!string.IsNullOrWhiteSpace(patage))
                {
                    return patage;
                }

                DateTime birthday = birdate.ToDateTime();
                DateTime NowDate = DateTime.Now;
                int age = 0;
                string UnitName = string.Empty;
                GlobalVar.BirthDateToAge(birthday, NowDate, ref age, ref UnitName);
                agestr = age + UnitName;
            }
            catch (Exception ex)
            {
                XCommon.XLog.LogError(ex);
            }
            return agestr;
        }


        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
 }

在UI中导入类型:

 xmlns:XQ="clr-namespace:XQ.View"

样式中定义转换器:

  <UserControl.Resources>
        <XQ:AgeTransformation x:Key="AgeTransformation"/>
    </UserControl.Resources>

DataGrid 列属性中设置绑定信息:

<DataGrid AlternatingRowBackground="AliceBlue" CanUserSortColumns="False" 
    ItemsSource="{Binding Path=DTPatientinfoDV}" AlternationCount="2" 
    AutoGenerateColumns="False" Background="White" BorderBrush="Gray" BorderThickness="1" FontSize="13.333" 
    HorizontalGridLinesBrush="Gray" IsEnabled="True" Name="DataGridSearch" VerticalGridLinesBrush="Gray" >
    <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding NUM}" Header="编号" IsReadOnly="True" SortMemberPath="NUM" Visibility="Hidden" Width="50" />                                  
            <DataGridTextColumn Binding="{Binding PATNAME}" Header="姓名" IsReadOnly="True" SortMemberPath="PATNAME" Width="70" />
            <DataGridTextColumn Binding="{Binding SEX}" Header="性别" IsReadOnly="True" SortMemberPath="SEX" Width="35" />
            <DataGridTextColumn  Header="年龄" IsReadOnly="True" Width="50">
            <DataGridTextColumn.Binding>
        	<MultiBinding Converter="{StaticResource AgeTransformation}">
	        	<Binding Path="PAT_AGE" ></Binding>
	        	<Binding Path="BIRTHDATE" StringFormat="yyyy-MM-dd"></Binding>                                             
        	</MultiBinding>
            </DataGridTextColumn.Binding>
       </DataGridTextColumn>                              
      </DataGrid.Columns>             
</DataGrid>

这样就可以使用.


备注:

如果绑定的数据源值为空或者没有对应的绑定列 显示的值属性比较怪异(显示成了:{DependencyProperty.UnsetValue}),

如图

暂时也没有找到解决的办法,如果有版本可以留言告诉我。



发表评论:

验证码