gpt4 book ai didi

c# - Xamarin 形成具有可绑定(bind)属性的 ValueConverter

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:22 26 4
gpt4 key购买 nike

我有一个 Xamarin 项目,我正在尝试绑定(bind)我的 ValueConverter。我在我的 UWP 项目中完成了此操作,但在 Xamarin 中我遇到了编译错误:

No property, bindable property, or event found for 'UserData', or mismatching type between value and property.

这样做的目的是当你在ListView中选择一个item时,entry读取UserValue属性,当你编辑这个entry中的文本时,item的属性的内容被更新,这个变化是在 ListView 中可见

这是我的代码:

MainPage.XAML:

<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>

MainPage.XAML.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;
}
}
}
}

方法.cs:

public class UserData : Prism.Mvvm.BindableBase
{
private bool _validatedByUser;
public bool ValidatedByUser
{
get
{
return _validatedByUser;
}
set
{
SetProperty(ref _validatedByUser, value);
}
}

private string _userName;
public string UserName
{
get
{
return _userName;
}
set
{
SetProperty(ref _userName, value);
}
}

private double _userValue;
public double UserValue
{
get
{
return _userValue;
}
set
{
SetProperty(ref _userValue, value);
}
}
}

public class ViewCell_Converter : BindableObject, IValueConverter
{
public UserData UserData
{
get { return (UserData)GetValue(bindableProperty); }
set { SetValue(bindableProperty, value); }
}

public static readonly BindableProperty bindableProperty = BindableProperty.Create(
nameof(UserData),
typeof(UserData),
typeof(ViewCell_Converter),
null);

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (UserData != null)
{
if (value is double _value)
{
if (UserData.ValidatedByUser)
{
return string.Format("{0:0.00}", _value);
}
else
{
return string.Format("{0:0.0000}", _value);
}
}
}

return null;
}

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

最佳答案

这是解决方案(快速示例):

    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();
}
}

您不需要更多的值转换器。

<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>

关于c# - Xamarin 形成具有可绑定(bind)属性的 ValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com