我想在自动填充字段中添加占位符颜色属性。我使用以下代码在我的表单中显示自动完成。我的Xmal代码如下,我使用xmlns:auto =" clr-namespace:XLabs.Forms.Controls; assembly = XLabs.Forms"。 我不能将placeholderColor属性绑定到我的xmal页面。但AutoCompleteViewModel包含placeholderColor属性的定义。如何解决此问题。
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Margin="20,10,20,0" BackgroundColor="#91BC47">
<auto:AutoCompleteView x:Name="Auto_Area" Text="Select Area" TextColor="White" IsVisible="True" Placeholder="select Area" PlaceholderColor="{Binding PlaceholderColor}"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
SearchCommand="{Binding SearchCommand}"
SearchTextColor="Yellow"
SelectedCommand="{Binding CellSelectedCommand}"
SelectedItem="{Binding SelectedItem}"
ShowSearchButton="False"
SuggestionBackgroundColor="#91BC47"
Margin="5,0,0,0"
SuggestionItemDataTemplate="{StaticResource SugestionItemTemplate}"
Suggestions="{Binding Items,
Mode=TwoWay}" />
</StackLayout>
public class AutoCompleteViewModel : XLabs.Forms.Mvvm.ViewModel
{
private ObservableCollection<AutoComplete> _items;
private Command<string> _searchCommand;
private Command<AutoComplete> _cellSelectedCommand;
private AutoComplete _selectedItem;
private Color _placeholderColor;
public AutoCompleteViewModel()
{
Items = new ObservableCollection<AutoComplete>();
Items = App._areas1.OrderBy(p => p).Select(p => new AutoComplete
{
Name = p,
ID = 1
}).ToObservableCollection();
}
public static BindableProperty PlaceholderColorProperty
= BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(EditorControl), Color.White);
public Color PlaceholderColor
{
get
{
return _placeholderColor;
}
set
{
SetProperty(ref _placeholderColor, value);
}
}
public ObservableCollection<AutoComplete> Items
{
get
{
return _items;
}
set
{
SetProperty(ref _items, value);
}
}
public Command<AutoComplete> CellSelectedCommand
{
get
{
return _cellSelectedCommand ?? (_cellSelectedCommand = new Command<AutoComplete>(parameter => Debug.WriteLine(parameter.ID + parameter.Name)));
}
}
public Command<string> SearchCommand
{
get
{
return _searchCommand ?? (_searchCommand = new Command<string>(
obj => { },
obj => !string.IsNullOrEmpty(obj.ToString())));
}
}
public AutoComplete SelectedItem
{
get
{
return _selectedItem;
}
set
{
SetProperty(ref _selectedItem, value);
}
}
}
答案 0 :(得分:1)
您的BindableProperty
需要添加到AutoCompleteView
而不是AutoCompleteViewModel
public class AutoCompleteView
{
public static BindableProperty PlaceholderColorProperty = BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(AutoCompleteView), Color.White);
public Color PlaceholderColor
{
get
{
return (Color)GetValue(PlaceholderColorProperty);
}
set
{
SetValue(PlaceholderColorProperty, value);
}
}
}
然后它只需要绑定到ViewModel中的PlaceholderColor。
public class AutoCompleteViewModel : XLabs.Forms.Mvvm.ViewModel
{
private Color _placeholderColor = Color.White;
public Color PlaceholderColor
{
get
{
return _placeholderColor;
}
set
{
SetProperty(ref _placeholderColor, value);
}
}
}