如何预填充自动完成 SelectedItem
How to prepopulate Autocomplete SelectedItem
从我之前的 post 开始,它有助于确定如何绑定到所选项目, 但现在我正试图增强该逻辑。
我正在尝试在初始化视图时预选项目。我尝试了多种选择,但似乎无法预选项目。我可以得到一些帮助吗?我当前的代码如下
关键字Class
public class Keyword : ObservableObject
{
private string _value;
public string Value
{
get { return _value; }
set { SetProperty(ref _value, value); }
}
}
视图模型
private ObservableCollection<object> _selectedKeywords = new ObservableCollection<object>();
private ObservableCollection<Keyword> _keywords = new ObservableCollection<Keyword>();
public TestViewModel()
{
Keywords = new ObservableCollection<Keyword>()
{
new Keyword { Value = "Apples" },
new Keyword { Value = "Bananas" },
new Keyword { Value = "Celery" }
};
SelectedKeywords = new ObservableCollection<object>(Keywords.Where(x => x.Value == "Apples"));
}
public ObservableCollection<object> SelectedKeywords
{
get { return _selectedKeywords; }
set { SetProperty(ref _selectedKeywords, value); }
}
public ObservableCollection<Keyword> Keywords
{
get { return _keywords; }
set { SetProperty(ref _keywords, value); }
}
查看
<autocomplete:SfAutoComplete MultiSelectMode="Token"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand"
TokensWrapMode="Wrap"
Text="{Binding Keyword, Mode=TwoWay }"
IsSelectedItemsVisibleInDropDown="false"
Watermark="Add..."
HeightRequest="120"
SelectedItem="{Binding SelectedKeywords}"
DataSource="{Binding Keywords}">
</autocomplete:SfAutoComplete>
要使其在您的视图模型中预选,请为您在视图上绑定的绑定设置一个值,基本上为 SelectedKeywords
分配一个值
类似于:
SelectedKeywords = Keywords.FirstOrDefault();
您可能需要双向绑定,但不确定,因为从未使用过此控件:
SelectedItem="{Binding SelectedKeywords, Mode=TwoWay}"
我们已经从您的代码片段中准备了示例,您没有在代码片段中添加 DisplayMemberPath 属性。请从以下位置找到示例。
http://www.syncfusion.com/downloads/support/directtrac/general/ze/AutoCompleteSample-270923957.zip
注意:我为Syncfusion工作。
此致,
达纳塞卡
从我之前的 post 开始,它有助于确定如何绑定到所选项目,
我正在尝试在初始化视图时预选项目。我尝试了多种选择,但似乎无法预选项目。我可以得到一些帮助吗?我当前的代码如下
关键字Class
public class Keyword : ObservableObject
{
private string _value;
public string Value
{
get { return _value; }
set { SetProperty(ref _value, value); }
}
}
视图模型
private ObservableCollection<object> _selectedKeywords = new ObservableCollection<object>();
private ObservableCollection<Keyword> _keywords = new ObservableCollection<Keyword>();
public TestViewModel()
{
Keywords = new ObservableCollection<Keyword>()
{
new Keyword { Value = "Apples" },
new Keyword { Value = "Bananas" },
new Keyword { Value = "Celery" }
};
SelectedKeywords = new ObservableCollection<object>(Keywords.Where(x => x.Value == "Apples"));
}
public ObservableCollection<object> SelectedKeywords
{
get { return _selectedKeywords; }
set { SetProperty(ref _selectedKeywords, value); }
}
public ObservableCollection<Keyword> Keywords
{
get { return _keywords; }
set { SetProperty(ref _keywords, value); }
}
查看
<autocomplete:SfAutoComplete MultiSelectMode="Token"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand"
TokensWrapMode="Wrap"
Text="{Binding Keyword, Mode=TwoWay }"
IsSelectedItemsVisibleInDropDown="false"
Watermark="Add..."
HeightRequest="120"
SelectedItem="{Binding SelectedKeywords}"
DataSource="{Binding Keywords}">
</autocomplete:SfAutoComplete>
要使其在您的视图模型中预选,请为您在视图上绑定的绑定设置一个值,基本上为 SelectedKeywords
类似于:
SelectedKeywords = Keywords.FirstOrDefault();
您可能需要双向绑定,但不确定,因为从未使用过此控件:
SelectedItem="{Binding SelectedKeywords, Mode=TwoWay}"
我们已经从您的代码片段中准备了示例,您没有在代码片段中添加 DisplayMemberPath 属性。请从以下位置找到示例。
http://www.syncfusion.com/downloads/support/directtrac/general/ze/AutoCompleteSample-270923957.zip
注意:我为Syncfusion工作。
此致,
达纳塞卡