我想在集合視圖中有一個預先選擇的專案,但它不起作用。正在將資料從視圖模型發送到集合視圖。我想在集合視圖中預選一項。我需要 xamarin 方面的幫助。這是我的代碼
xml
<CollectionView x:Name="WorkerFlockView" ItemsSource="{Binding WorkerFlockDetails}" SelectedItem="{Binding SelectedFlock}" SelectionMode="Single" SelectionChangedCommand="{Binding ViewFlockControlPointsCommand}" HeightRequest="100">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="10" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Frame Margin="0,0,0,0" Padding="0" HorizontalOptions="CenterAndExpand" BackgroundColor="#F7F7F7" Opacity="0.93" HasShadow="{OnPlatform Android=true, iOS=false}" CornerRadius="0" IsClippedToBounds="True" >
<StackLayout Orientation="Vertical" Opacity="1">
<StackLayout HeightRequest="15" BackgroundColor="#105CA5"></StackLayout>
<StackLayout Orientation="Vertical" Padding="10,0,10,0" VerticalOptions="CenterAndExpand">
<StackLayout Orientation="Horizontal">
<Label Text="Flock" TextColor="#105CA5"></Label>
<Label Text="{Binding Flock_name}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Farm" TextColor="#105CA5"></Label>
<Label Text="{Binding Farm_name}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="House" TextColor="#105CA5"></Label>
<Label Text="{Binding House_name}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Age" TextColor="#105CA5"></Label>
<Label Text="{Binding Flock_age}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Bird Type" TextColor="#105CA5"></Label>
<Label Text="{Binding Bird_type}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Number of birds" TextColor="#105CA5"></Label>
<Label Text="{Binding Number_of_birds}"></Label>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
<VisualStateManager.VisualStateGroups >
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" />
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Blue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
查看模型
類 HomeWorkerViewModel :INotifyPropertyChanged {
public HomeWorkerViewModel(){
WorkerFlockDetails = new ObservableCollection<WorkerFlock>();
selectedFlock = WorkerFlockDetails.Skip(3).FirstOrDefault();
//function for adding information
Init();
}
//Populate WorkerFlockDetails with data
function Init()
public ObservableCollection<WorkerFlock> workerFlockDetails;
public ObservableCollection<WorkerFlock> WorkerFlockDetails
{
get => workerFlockDetails;
set
{
workerFlockDetails = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WorkerFlockDetails)));
}
}
private WorkerFlock selectedFlock { get; set; }
public WorkerFlock SelectedFlock
{
get { return selectedFlock; }
set
{
if (selectedFlock != value)
{
try
{
//Feed = selectedFlock.Feed_intake.ToString() " Kg";
selectedFlock = value;
}
catch
{
}
}
}
}
}
uj5u.com熱心網友回復:
有幾件事:
ObservableCollection類自動實作了一個 Notify 方法,因此您不需要私有欄位并呼叫
PropertyChanged公共 ObservableCollection WorkerFlockDetails { 獲取;放; }
修改時應該呼叫 PropertyChanged
SelectedFlockprivate WorkerFlock selectedFlock; public WorkerFlock SelectedFlock { get => selectedFlock set { if (selectedFlock != value) { try { //Feed = selectedFlock.Feed_intake.ToString() " Kg"; selectedFlock = value; } catch { } finally { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedFlock))); } } } }讓我們轉到建構式。如果該
Init()方法用資料填充您的 ObservableCollection,您應該在選擇 Item 之前呼叫它,否則集合將為空
并且在使用代碼更改選擇時,您應該呼叫公共屬性而不是私有屬性,因為它會通知 UI 它已更改
public HomeWorkerViewModel(){
WorkerFlockDetails = new ObservableCollection<WorkerFlock>();
//Call init before selection
//function for adding information.
Init();
//Use public property
SelectedFlock = WorkerFlockDetails.Skip(3).FirstOrDefault();
}
快樂編碼!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313139.html
標籤:C# xml 沙马林 xamarin.forms
