c# - 如何为多个转换编写 Iconverter
问题描述
下面是我用来在绑定到列表视图之前转换值的代码。但是这里只有前 2 个转换工作,convert3 和 convert4 的结果没有显示。请帮助我
<ContentPage.Resources>
<local:Class1 x:Key="_converter"/>
</ContentPage.Resources>
<ContentPage.Content>
<ListView x:Name="Models">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding from_time,Converter={StaticResource _converter}}"/>
<Label Text="{Binding to_time,Converter={StaticResource _converter}}"/>
<Label Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
<Label Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
public class Class1: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var str = (string)value; // value is the binding data
if (str== "00:00:00.0000000")
return "";
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
解决方案
由于它适用于第一个和第二个标签,我认为问题是由布局引起的。在您的情况下,StackLayout在运行时不会影响其子元素的大小。
你可以使用Grid。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0" HeightRequest="30" Text="{Binding from_time,Converter={StaticResource _converter}}"/>
<Label Grid.Row="1" HeightRequest="30" Text="{Binding to_time,Converter={StaticResource _converter}}"/>
<Label Grid.Row="2" HeightRequest="30" Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
<Label Grid.Row="3" HeightRequest="30" Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
</Grid>
推荐阅读
- java - 如何返回具有该方法的枚举参数的 HashMap?
- angular - Angular 12中的同步融合图与数据绑定
- facebook - Facebook 登录对话框未反映我设置的所有权限
- javascript - 如果在选择其他选项时选择了一个选项,则显示警报消息
- android - 使用 resolveActivity 检查 Intent 是否可以处理
- javascript - 如何在引导 vue 中制作粘性标题表
- python - 如何定义一个计算单词重复次数的函数
- scala - 随着时间的推移,火花变得越来越慢
- animation - 如何将两个不同的正弦波合并为一个动画?
- c# - 无法获取“AxSixBarcodeReader”控件的窗口句柄。不支持无窗口 ActiveX 控件。请帮帮我