【发布时间】:2020-11-11 15:03:09
【问题描述】:
下面是我用来在绑定到列表视图之前转换值的代码。但是这里只有前 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();
}
}
【问题讨论】:
-
由于每个转换中的逻辑相似,您可以在同一个转换中处理它们。 class1 中的格式是非法的。
-
@LucasZhang-MSFT 先生,“已经更改了 class1 代码,但标签 3 和 4 的值也没有显示出来。请帮助我,谢谢
-
你可以分享一个样本,这样我就可以直接在我这边测试了。
-
@LucasZhang-MSFT,先生,我已经更新了代码,请帮助我。谢谢
标签: c# xamarin cross-platform