WPF值转换器

总是记不住,放上来方便复制

单个参数

[ValueConversion(typeof(int), typeof(string))]
public class SexConverter : IValueConverter

{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int sex = System.Convert.ToInt32(value);
switch (sex)
{
case 1:
return "男";
case 2:
return "女";
case 0:
return "未知";
default:
return "";
}
}

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

 

 

<UserControl.Resources>
<convert:SexConverter x:Key="SexConverter" ></convert:SexConverter>
</UserControl.Resources>

 

 

<TextBlock HorizontalAlignment="Left" MinWidth="100" Text="{Binding Sex,Converter={StaticResource SexConverter}}">

多个参数

public class RichBorderWidthConverter : IMultiValueConverter

{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return 200;
}

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

 

 

<Grid>
<Grid.Width>
<MultiBinding Converter="{StaticResource RichBorderWidthConverter}">
<Binding Path="xx"/>
<Binding Path="yy"/>
</MultiBinding>
</Grid.Width>


</Grid>

posted @ 2021-10-14 10:23  奇迹之耀  阅读(66)  评论(0编辑  收藏  举报