我有一个Xamarin项目,并且我试图在ValueConverter上绑定一个对象。 我在UWP项目中完成了此任务,但在Xamarin中却遇到了编译错误:
找不到“ UserData”的属性,可绑定属性或事件,或者 值和属性之间的类型不匹配。
这样做的目的是,当您在ListView中选择一个项目时,该条目将读取UserValue属性,当您编辑此条目中的文本时,该项目的属性的内容将更新,并且此更改为在ListView中可见。
有我的代码:
MainPage.XAML:
<input type="radio" name="radio1" id="radio1" required />
<input type="radio" name="radio1" id="radio2" />
<div>Text to be green if radio2 is checked</div>
MainPage.XAML.cs:
<StackLayout>
<Entry x:Name="Entry_NewValue" TextChanged="Entry_NewValue_TextChanged"/>
<ListView x:Name="_ListView" ItemSelected="_ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.Resources>
<local:ViewCell_Converter x:Key="ViewCell_Converter" UserData="{Binding}"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding UserName}"/>
<Label Grid.Column="1" Text="{Binding UserValue, Converter={StaticResource ViewCell_Converter}}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Methods.cs:
public MainPage()
{
InitializeComponent();
_ListView.ItemsSource = UserDatas();
}
private ObservableCollection<UserData> UserDatas()
{
return new ObservableCollection<UserData>()
{
new UserData() { UserName = "Name1", UserValue = 10, ValidatedByUser = true, },
new UserData() { UserName = "Name2", UserValue = 11, ValidatedByUser = false, },
new UserData() { UserName = "Name3", UserValue = 12, ValidatedByUser = true, },
new UserData() { UserName = "Name4", UserValue = 13, ValidatedByUser = false, },
new UserData() { UserName = "Name5", UserValue = 14, ValidatedByUser = true, },
};
}
private void _ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem is UserData userData)
{
Entry_NewValue.Text = userData.UserValue.ToString();
}
}
private void Entry_NewValue_TextChanged(object sender, TextChangedEventArgs e)
{
if (_ListView.SelectedItem is UserData userData)
{
if (double.TryParse(Entry_NewValue.Text, out double _double))
{
if(userData.UserValue != _double)
{
userData.UserValue = _double;
}
}
}
}
答案 0 :(得分:0)
这是重新布置(快速示例):
public class ViewCell_Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double doubleValue)
{
if (value is double _value)
{
return string.Format("{0:0.00}", doubleValue);
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您不需要更多的Valueconverter。
<StackLayout>
<Entry x:Name="Entry_NewValue" TextChanged="Entry_NewValue_TextChanged"/>
<ListView x:Name="_ListView" ItemSelected="_ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.Resources>
<local:ViewCell_Converter x:Key="ViewCell_Converter"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding UserName}"/>
<Label Grid.Column="1" Text="{Binding UserValue, Converter={StaticResource ViewCell_Converter}}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>