近期在項目希望實現 編輯值與原始數據的 雙向轉換。 經過調試,這才發現Converter自帶該功能。 以下是一個簡單案例。
1.創建數據實體
namespace WpfApp_converter.Model
{
public class DataEntity
{
public int id { get; set; }
public bool useFlag { get; set; }
public string deviceName { get; set; }
}
}
2.定義轉換器
using System;
using System.Windows.Data;
using System.Windows.Markup;
namespace WpfApp_converter.Converter
{
public class BoolToStrConverter : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object obj = value;
if (obj is bool)
{
bool path = (bool)obj;
if (path == true)
{
return "是";
}
else
{
return "否";
}
}
else
{
return "";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(Boolean))
{
return true;
}
else
{
if (value.ToString() == "是")
{
return true;
}
else if (value.ToString() == "否")
{
return false;
}
else
{
value = "是";
return true;
}
}
}
}
}
3.在前端.xaml裏使用
<Window x:Class="WpfApp_converter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:my="clr-namespace:WpfApp_converter.Converter"
Height="500" Width="1000" WindowStartupLocation="CenterScreen" Title="設備狀態維護">
<Grid>
<Grid.Resources>
<my:BoolToStrConverter x:Key="BoolToStrConverter" />
</Grid.Resources>
<Border BorderBrush="Black" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200*"></RowDefinition>
<RowDefinition Height="70"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" CanUserAddRows="False" AutoGenerateColumns="False" Margin="0"
SelectionMode="Single" SelectionUnit="FullRow" EnableRowVirtualization="False"
FontSize="15"
Name="dataGrid1" RowHeaderWidth="0" Background="{x:Null}" BorderBrush="LightGray" RowHeight="31">
<DataGrid.Columns>
<DataGridTextColumn Header="編號" Binding="{Binding id}" Width="70*" />
<DataGridTextColumn Header="設備名稱" Binding="{Binding deviceName}" Width="70*" />
<DataGridTextColumn Header="是否啓用"
Binding="{Binding Path= useFlag, Converter={StaticResource BoolToStrConverter}}" Width="110*" />
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="1">
<Button Content="獲取數據狀態" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" Width="162" Height="26" Click="Button_Click" />
</Grid>
</Grid>
</Border>
</Grid>
</Window>
4.後端xaml.cs文件裏賦值和調用
賦值:
using System;
using System.Collections.Generic;
using System.Windows;
using WpfApp_converter.Model;
namespace WpfApp_converter
{
public partial class MainWindow : Window
{
private List<DataEntity> globalEntityList = new List<DataEntity>();
public MainWindow()
{
InitializeComponent();
loadDataToGrid();
}
private void loadDataToGrid()
{
for (int i = 0; i < 10; i++)
{
globalEntityList.Add(new DataEntity()
{
id = i + 1,
deviceName = "檢修設備" + (i + 1),
useFlag = i % 3 == 0 ? false : true,
});
}
dataGrid1.ItemsSource = globalEntityList;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dataGrid1.ItemsSource = null;
dataGrid1.ItemsSource = globalEntityList;
globalEntityList.ForEach(t =>
{
Console.WriteLine("\n" + t.deviceName + " " + t.useFlag);
});
}
}
}