- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这个属性有什么意义?添加后,我仍然需要对值对象进行转换。
[ValueConversion(sourceType: typeof(double), targetType: typeof(string))]
public class SpeedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var speed = (double)value;
难道只是为了代码可读性?因为当我在 xaml 中将绑定(bind)的路径更改为 String 时,Visual Studio 不会发出有关类型不正确的警告,并且仅在转换时才会抛出异常,因此即使在编译时早期错误捕获中也没有任何意义。我也可以将转换更改为字符串,尽管它与此属性冲突,但不会引发警告。
最佳答案
您可以潜在地使用 ValueConversionAttribute
来确定转换器中涉及的类型,并有效地使用该信息。看Piping Value Converters in WPF作为使用 ValueConversionAttribute
的一个很好的例子。
该示例显示了如何链接多个转换器类,并且 ValueConversion 可用于将类型信息传递给行中的下一个转换器。
[ValueConversion( typeof( string ), typeof( ProcessingState ) )]
public class IntegerStringToProcessingStateConverter : IValueConverter
{
object IValueConverter.Convert(
object value, Type targetType, object parameter, CultureInfo culture )
{
int state;
bool numeric = Int32.TryParse( value as string, out state );
Debug.Assert( numeric, "value should be a String which contains a number" );
Debug.Assert( targetType.IsAssignableFrom( typeof( ProcessingState ) ),
"targetType should be ProcessingState" );
switch( state )
{
case -1:
return ProcessingState.Complete;
case 0:
return ProcessingState.Pending;
case +1:
return ProcessingState.Active;
}
return ProcessingState.Unknown;
}
object IValueConverter.ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotSupportedException( "ConvertBack not supported." );
}
}
// *************************************************************
[ValueConversion( typeof( ProcessingState ), typeof( Color ) )]
public class ProcessingStateToColorConverter : IValueConverter
{
object IValueConverter.Convert(
object value, Type targetType, object parameter, CultureInfo culture )
{
Debug.Assert(value is ProcessingState, "value should be a ProcessingState");
Debug.Assert( targetType == typeof( Color ), "targetType should be Color" );
switch( (ProcessingState)value )
{
case ProcessingState.Pending:
return Colors.Red;
case ProcessingState.Complete:
return Colors.Gold;
case ProcessingState.Active:
return Colors.Green;
}
return Colors.Transparent;
}
object IValueConverter.ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture )
{
throw new NotSupportedException( "ConvertBack not supported." );
}
}
object IValueConverter.Convert(
object value, Type targetType, object parameter, CultureInfo culture )
{
object output = value;
for( int i = 0; i < this.Converters.Count; ++i )
{
IValueConverter converter = this.Converters[i];
Type currentTargetType = this.GetTargetType( i, targetType, true );
output = converter.Convert( output, currentTargetType, parameter, culture );
// If the converter returns 'DoNothing'
// then the binding operation should terminate.
if( output == Binding.DoNothing )
break;
}
return output;
}
//***********Usage in XAML*************
<!-- Converts the Status attribute text to a Color -->
<local:ValueConverterGroup x:Key="statusForegroundGroup">
<local:IntegerStringToProcessingStateConverter />
<local:ProcessingStateToColorConverter />
</local:ValueConverterGroup>
关于c# - ValueConversionAttribute 类的重点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926226/
我希望我在正确的轨道上吗?该网站位于: http://www.dinewine.com/startbootstrap-agency-1.0.6/index.html 当鼠标悬停在顶部导航时,鼠标悬停背
我正在做一个家庭作业,其中包含 Peterson 的领导者选举算法。我很困惑为什么你需要这样的算法来选择领导者。为什么不随机选择一个?或者队列中的第一个。 任何澄清都会很好,提前致谢 最佳答案 尝试思
我最近尝试执行以下行; string strCmdText; strCmdText = "netstat -np TCP | find " + quote + number + quote + "";
好的,我已经订购了 Applying Domain-Driven Design and Patterns: Using .Net ,但是在我等待它到来的时候,我正在考虑开始在我当前的项目中应用这些技术
我正在尝试使用 jQuery prop() 方法禁用元素(表单字段)。有两个字段,一个叫做fee,一个叫做currency。每当 fee 设置为 0 时,第二个字段 currency 将被禁用。这样做
下面的代码是我用来在我的应用程序中创建 GridView 的代码。但问题是列表选择器的宽度和高度不等于图标的宽度和高度(图 1)。图片 #2 是我想要完成的。 最佳答案 尝试使用 android:li
我是一名优秀的程序员,十分优秀!