我为它创建了一个line=$(awk -f '/nel/{print NR}' FILENAME}
echo "$line"
和绑定系统枚举值。并且还为它创建了一个值转换器,使其更具人性化。
但是当我在ComboBox
中选择项目时,它会显示错误并在控制台中转储一些错误消息
知道怎么解决这个问题吗?
我还尝试为ComboBox
设置另一个值转换器,但它也不起作用。
cs文件:
SelectedItem
xaml文件:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Ports;
using System.Linq;
using System.Windows;
using System.Windows.Data;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private StopBits _stopBits;
public StopBits SelectedStopBits
{
get { return _stopBits; }
set { _stopBits = value; }
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
public class DataConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var tmp = (int[])value;
var convert = new Dictionary<StopBits, string>()
{
{StopBits.None, "none"},
{StopBits.One, "1 bit"},
{StopBits.Two, "2 bit"},
{StopBits.OnePointFive, "1.5 bit"}
};
return tmp.Select(item => convert[(StopBits)item]).ToList();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
错误:
<Window x:Class="WpfApp1.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"
xmlns:local="clr-namespace:WpfApp1"
xmlns:serialPort="clr-namespace:System.IO.Ports;assembly=System"
xmlns:provider="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="StopBitsProvider" MethodName="GetValues" ObjectType="{x:Type provider:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="serialPort:StopBits"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:DataConvert x:Key="DataConverter"/>
</Window.Resources>
<StackPanel>
<TextBlock Text="Stop Bits:"/>
<ComboBox ItemsSource="{Binding Source={StaticResource StopBitsProvider}, Converter={StaticResource DataConverter}}"
SelectedItem="{Binding SelectedStopBits}"/>
</StackPanel>
</Window>
答案 0 :(得分:1)
您需要将SelectedItem
从string
转换为StopBits
值。
因此,应用于ConvertBack
绑定的转换器的SelectedItem
方法应返回一个枚举值,该属性实际上可以设置为:
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
StopBits enumValue = default(StopBits);
if (value != null)
Enum.TryParse(value.ToString(), out enumValue);
return enumValue;
}
您无法将StopBits
属性设置为string
。
编辑或者你可以从转换器中返回字典:
public class DataConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var tmp = (int[])value;
var convert = new Dictionary<StopBits, string>()
{
{StopBits.None, "none"},
{StopBits.One, "1 bit"},
{StopBits.Two, "2 bit"},
{StopBits.OnePointFive, "1.5 bit"}
};
return convert;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
...并稍微修改你的XAML:
<ComboBox ItemsSource="{Binding Source={StaticResource StopBitsProvider}, Converter={StaticResource DataConverter}}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding SelectedStopBits}"/>