wpf 数据转换器Convert的基本使用

使用场景

        数据实体中常有用INT值属性保存对应该枚举的数据,在界面显示时想显示枚举的描述时使用。

效果

示例

        实体

/// <summary>
/// 点位表 实体
/// </summary>
[MfTable("Point")]
public class PointModel : BaseModelExtendIdAndName<int>
{
	/// <summary>
	/// 编号
	/// </summary>		
	public int No { get; set; }

	/// <summary>
	/// X轴坐标
	/// </summary>		
	public decimal PointX { get; set; }

	/// <summary>
	/// Y轴坐标
	/// </summary>		
	public decimal PointY { get; set; }

    /// <summary>
    /// 另外增加的修正值X
    /// </summary>
    public decimal AddX { get; set; }

    /// <summary>
    /// 另外增加的修正值Y
    /// </summary>
    public decimal AddY { get; set; }

    /// <summary>
    /// 类型[焊接点位:0,相机点位:1,标定点位:2,轴点位:3] PointTypeEnum
    /// </summary>		
    public int Type { get; set; }

	/// <summary>
	/// 配方ID
	/// </summary>
	public int FormulaId { get; set; }

	/// <summary>
	/// 轴卡 ID
	/// </summary>
	public int AxisId { get; set; }

    /// <summary>
    /// 获取X轴总值
    /// </summary>
    /// <returns></returns>
    public decimal GetPointXSum()
    {
        return PointX + AddX;
    }

    /// <summary>
    /// 获取Y轴总值
    /// </summary>
    /// <returns></returns>
    public decimal GetPointYSum()
    {
        return PointY + AddY;
    }

    /// <summary>
    /// 获取类型描述
    /// </summary>
    /// <returns></returns>
    public string GetPointTypeDesc()
    {
        return ((PointTypeEnum)Enum.Parse(typeof(PointTypeEnum), this.Type.ToString())).GetEnumDesc();
    }
}    

/// <summary>
/// 点位类型
/// </summary>
public enum PointTypeEnum
{
	/// <summary>
	/// 焊接点位
	/// </summary>
	[Description("焊接点位")]
	WeldPoint = 0,

	/// <summary>
	/// 相机点位
	/// </summary>
	[Description("相机点位")]
	CameraPoint = 1,

    /// <summary>
    /// 标定点位
    /// </summary>
    [Description("标定点位")]
    StandardizationPoint = 2,

    /// <summary>
    /// 轴点位
    /// </summary>
    [Description("轴点位")]
    AxisPoint = 3,

    /// <summary>
    /// CAD图点位
    /// </summary>
    [Description("CAD图点位")]
    CADPoint = 4,
}

转换器

/// <summary>
/// 转换器
/// </summary>
public class PointTypeConvert : IValueConverter
{
    //正转
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //根据枚举值获取对应的描述
        return ((PointTypeEnum)value).GetEnumDesc();
    }

    //反转
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

界面绑定

注意看

1.引用转换器所在命名空间 xmlns:dc="clr-namespace:SZCLKJ.Weld.App.DataConvert"

2.在界面资源中创建资源key :<dc:PointTypeConvert x:Key="pointTypeCon"/>

3.在要使用的地方调用: <DataGridTextColumn Header="类型" Binding="{Binding Type,Converter={StaticResource pointTypeCon}}" Width="100"/>

<UserControl x:Class="SZCLKJ.Weld.App.Views.PointListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:SZCLKJ.Weld.App.Views"
             xmlns:dc="clr-namespace:SZCLKJ.Weld.App.DataConvert"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SZCLKJ.Weld.App;component/Asset/Styles/BaseResources.xaml"/>
                <ResourceDictionary Source="/SZCLKJ.Weld.App;component/Asset/Styles/ButtonStyles.xaml"/>
                <ResourceDictionary Source="/SZCLKJ.Weld.App;component/Asset/Styles/TextBoxStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <dc:PointTypeConvert x:Key="pointTypeCon"/>
        </ResourceDictionary>        
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <!--<Border BorderBrush="#DDD" BorderThickness="0,0,0,1"/>-->
        <WrapPanel VerticalAlignment="Center" >
            <!--搜索框-->
            <Label Grid.Row="0" Content="配方:" BorderThickness="0" FontSize="20" Height="35" Margin="10,0,0,0" 
                HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#AAFFFFFF" />
            <ComboBox Grid.Row="0" x:Name="cboFormulaList" 
                ItemsSource="{Binding FormulaList}" DisplayMemberPath="Name" SelectedValuePath="Id" 
                SelectedItem="{Binding SearchModel.FormulaId, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                SelectionChanged="cboFormulaList_SelectionChanged"
                Width="150" Height="35" FontSize="20" Margin="10,10,0,6" HorizontalAlignment="Left" VerticalAlignment="Center" />
            <Label Content="点位名称:" BorderThickness="0" FontSize="20" Height="35" Margin="10,0,0,0" 
                   VerticalAlignment="Center" 
                   Foreground="#AAFFFFFF" />
            <TextBox Text="{Binding SearchModel.NameLike}" VerticalAlignment="Center" HorizontalAlignment="Left" 
                    Width="100" Height="35" Margin="10,0,0,0" 
                     Style="{DynamicResource SearchTextBoxStyle}"/>
            <Label Content="类型:" BorderThickness="0" FontSize="20" Height="35" Margin="10,0,0,0" 
                    HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#AAFFFFFF" />
            <!--<ComboBox x:Name="cboPointTypes" 
                ItemsSource="{Binding PointTypes}" DisplayMemberPath="Name" SelectedValuePath="Id" 
                SelectedItem="{Binding SearchModel.Type}" 
                Width="150" Height="35" FontSize="20" Margin="10,10,0,6" HorizontalAlignment="Left" 
                VerticalAlignment="Center" />-->

            <ComboBox Name="cboPointType" Width="150" Height="35" FontSize="20" Margin="10,10,0,6" 
                      HorizontalAlignment="Left" VerticalAlignment="Center"
                ItemsSource="{Binding PointTypes}"
                DisplayMemberPath="Description"
                SelectedValuePath="Val"
                SelectedItem="{Binding PointTypeSelect, Mode=TwoWay}"
            >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding PointTypeSelectionChangedCommand}"   
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
        </WrapPanel>
        
        <!--右边按钮-->
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal">
            <Button Content="查询" Style="{StaticResource NormalButtonStyle}" Background="#FF0ABEFF" 
                    Width="70" Margin="5,0" Command="{Binding QueryCommand}"/>
            <Button Content="添加" 
                    Style="{StaticResource IconWithContentButtonStyle}" Width="100"
                    Tag="&#xe603;" Background="#FFF05005"
                    Command="{Binding AddCommand}"/>    
        </StackPanel>
        <DataGrid Grid.Row="1" ItemsSource="{Binding DataList}" AutoGenerateColumns="False" CanUserAddRows="False" ColumnWidth="*" 
            >
            <DataGrid.Columns>
                <DataGridTextColumn Header="序号" Binding="{Binding No}" Width="150" />
                <DataGridTextColumn Header="名称" Binding="{Binding Name}" Width="100" />
                <DataGridTextColumn Header="X坐标" Binding="{Binding PointX}" Width="100"/>
                <DataGridTextColumn Header="Y坐标" Binding="{Binding PointY}" Width="100"/>
                <DataGridTextColumn Header="补偿X" Binding="{Binding AddX}" Width="100"/>
                <DataGridTextColumn Header="补偿Y" Binding="{Binding AddY}" Width="100"/>
                <!--<DataGridTextColumn Header="类型" Binding="{Binding TypeDesc}" Width="100"/>-->
                <DataGridTextColumn Header="类型" Binding="{Binding Type,Converter={StaticResource pointTypeCon}}" Width="100"/>
                <DataGridTextColumn Header="备注" Binding="{Binding Remark}"/>
                <DataGridTemplateColumn Width="120" Header="操作">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Button x:Name="gridEdit"
                                        Width="40"
                                        Height="20"
                                        Background="#409EFF"
                                        Foreground="White"
                                        Margin="10,0,0,0"
                                        Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
                                        CommandParameter="{Binding Id}"
                                        Content="编辑" />

                                <Button
                                        x:Name="gridDele"
                                        Width="40"
                                        Height="20"
                                        Background="#F56C6C"
                                        Foreground="White"
                                        Margin="10,0,0,0"
                                        Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
                                        CommandParameter="{Binding Id}"
                                        Content="删除" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StevenChen85

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值