如何解决如何获得x:控件名称以在Xamarin Forms的ViewModel中使用? 在ViewModel中
我正在对Entry进行自定义控件,我需要获取x:Entry的名称才能在项目的viewmodel中使用它,以便在使用它时获得光标位置。
这是我的代码:
这是我自定义控件的xaml:
<Entry
x:Name="EntryControl"
Margin="45,0"
Placeholder="{Binding Source={x:Reference CKEditorView},Path=Placeholder}"
Text="{Binding EntryText}"
WidthRequest="320" />
我想获得此x:Name在viewmodel中使用。
解决方法
正如@Jason所说,您可以绑定 CursorPosition 的值。
该属性是可绑定的,因此我们可以将其设置为
<Entry
CursorPosition="{Binding Position,Mode=TwoWay}"
x:Name="EntryControl"
Margin="45,0"
Placeholder="{Binding Source={x:Reference CKEditorView},Path=Placeholder}"
Text="{Binding EntryText}"
WidthRequest="320" />
在ViewModel中
public class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<Color> MySource { get; set; }
int position;
public int Position
{
get
{
return position;
}
set
{
// it will been called when we edit the entry
if(position != value)
{
position = value;
OnPropertyChanged("Position");
// do some thing you want
}
}
}
public MainViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。