我正在做一個 Xamarin 表單專案,我有一個可以完成或完成的任務串列。我想添加一個按鈕來顯示已完成任務的“標記為已完成”和已完成任務的“標記為已完成”。當然,當用戶點擊一次時,它需要重繪 。我有一個“CompletedDate”欄位,它定義任務是否在具有價值時完成。
TextColor="{StaticResource PrimaryBlack}" />
<CollectionView Grid.Row="1" ItemsSource="{Binding Tasks}" x:Name="List3">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Frame
Margin="0,10"
Padding="10"
BackgroundColor="{StaticResource PrimaryWhite}"
BorderColor="{StaticResource PrimaryLightGray}"
CornerRadius="10"
HasShadow="False">
<Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto" RowSpacing="15">
<StackLayout Orientation="Horizontal">
<Label
FontFamily="{StaticResource MeduimFont}"
Style="{StaticResource LabelMedium}"
Text="Completed"
IsVisible="{Binding CompletedTaskVisibility}}"
TextColor="{Binding PrimaryPersianGreen}" />
<StackLayout HorizontalOptions="EndAndExpand" Orientation="Horizontal">
<Image
HeightRequest="20"
Source="iconCalender.png"
WidthRequest="20" />
<Label
FontFamily="{StaticResource MeduimFont}"
Style="{StaticResource LabelMedium}"
Text="{Binding CompletedDate,StringFormat='{0:MMMM dd, yyyy}'}"
TextColor="{StaticResource PrimaryBlack}"
/>
</StackLayout>
</StackLayout>
<BoxView
Grid.Row="1"
HeightRequest="1"
Color="{StaticResource PrimaryLightGray}" />
<Label
Grid.Row="2"
Style="{StaticResource LabelMedium}"
Text="{Binding Name}"
TextColor="{StaticResource PrimaryBlack}" />
<Label
Grid.Row="3"
Style="{StaticResource LabelMedium}"
Text="{Binding Description}"
TextColor="{StaticResource PrimaryBlack}" />
<Button
x:Name="lstbtnMarkasComplite"
Grid.Row="5"
Padding="15,0"
Clicked="MarkTaskAsCompletedClicked"
CornerRadius="20"
FontSize="{StaticResource Font12}"
HeightRequest="40"
CommandParameter="{Binding Id}"
HorizontalOptions="CenterAndExpand"
Style="{StaticResource ButtonPurple}"
Text="Mark as Completed" />
</Grid>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
我有一個“CompletedDate”欄位,它定義任務是否在具有價值時完成。
uj5u.com熱心網友回復:
有兩種解決方案:
- 創建一個布爾變數,用于檢查視圖模型中的 CompletedDate 并將 Text 系結到它。然后使用轉換器將布爾轉換為字串。關于 clicked 事件,你可以嘗試做和 Text 一樣的事情,或者在 page.cs 中檢查 click 事件。
- 也在視圖模型中創建一個布爾變數,但是您需要在 xaml 中創建兩個按鈕(一個是“標記為已完成”,另一個是“標記為已完成”)并將它們的 IsVisible 屬性系結到布爾變數中視圖模型。
我建議你嘗試第二種解決方案,因為第一種需要做很多事情并將點擊事件轉換為命令。第二個減少了很多作業。
最后,您可以嘗試使用其他一些控制元件來代替按鈕。如:
復選框:https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/checkbox
開關:https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/switch
編輯:
在視圖模型中:
public ViewModel()
{
CompletedDate = ""; //set the value with the model data
if (CompletedDate != null)
{
isCompleted = true;
}
else isCompleted = false;
}
public event PropertyChangedEventHandler PropertyChanged;
bool isCompleted;
public bool IsCompleted {
get { return isCompleted; }
set {
isCompleted = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
}
}
}
在 xaml 中:
<Button
x:Name="lstbtnMarkasComplite"
Grid.Row="5"
Padding="15,0"
Clicked="MarkTaskAsCompletedClicked"
CornerRadius="20"
FontSize="{StaticResource Font12}"
HeightRequest="40"
CommandParameter="{Binding Id}"
HorizontalOptions="CenterAndExpand"
Style="{StaticResource ButtonPurple}"
Text="Mark as Completed"
IsVisible="{Binding IsCompleted}"/>
<Button
x:Name="lstbtnMarkasComplite"
Grid.Row="5"
Padding="15,0"
Clicked="MarkTaskAsInCompletedClicked"
CornerRadius="20"
FontSize="{StaticResource Font12}"
HeightRequest="40"
CommandParameter="{Binding Id}"
HorizontalOptions="CenterAndExpand"
Style="{StaticResource ButtonPurple}"
Text="Mark as in Completed"
IsVisible="{Binding IsCompleted}/>
您也可以像 Ibrennan208 所說的那樣使用 DataTrigger,然后檢查 page.cs 中的 Text 值:
private async void Button_Clicked(object sender, EventArgs e)
{
Button button = sender as Button;
if(button.Text == "Mark as Completed")
{
.........
}
else
{
.........
}
}
更新:
<Button.Triggers>
<DataTrigger TargetType="Button" Binding="{Binding CompletedDate}" Value="{x:Null}">
<Setter Property="Text" Value="Mark Task as Completed"/>
</DataTrigger>
</Button.Triggers>
uj5u.com熱心網友回復:
您可以嘗試DataTrigger在您的Button. 它們可用于將條件系結或值應用于屬性,例如Button.Text屬性。
對于按鈕,您可以類似于以下方式實作它:
<Button Test="Mark Complete">
<Button.Triggers>
<DataTrigger TargetType="Button" Binding="{Binding IsCompleted}" Value="True">
<Setter Property="Text" Value="Mark Incomplete"/>
</DataTrigger>
</Button.Triggers>
</Button>
然后您還可以將該IsCompleted欄位添加到您的視圖模型中:
public bool IsCompleted
get { return CompletedDate.HasValue; }
這將充當您的按鈕的開關。
您還需要為IsCompleted變數添加通知。
OnPropertyChanged(nameof(IsCompleted))在你設定時呼叫CompletedDate會做到這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473725.html
標籤:C# xml xamarin xamarin.forms 数据绑定
下一篇:字體真棒代碼作為字串未按預期呈現
