我正在嘗試制作功能,以便在用戶單擊“增加字體大小”按鈕時在整個應用程式中為用戶制作更大的文本。使用 MVVM,我已經這樣做了:
- 增加字體大小按鈕單擊
- 增加系結到布局中幾乎每個文本的雙欄位“fontSize”的值
- 單擊按鈕后使用新值更新 UI
但是我不知道如何在 Collectionview 中實作這一點,我在 .xaml 檔案中有系結,并帶有一些特定的串列(專案是模型類)。collectionview DataTemplate 包含我想增加字體大小的標簽。有沒有辦法在我的模型類中添加“fontSize”欄位。如果不是,如何使用增加字體大小的“新”串列更新 UI。我感謝任何幫助、提示和討論。謝謝你。
uj5u.com熱心網友回復:
您可以在視圖模型中創建 bindableproperty(fontsize) 并使用相對系結,以便 Collectionview 中的標簽可以更改其字體大小,代碼如下:
視圖模式:
public class ColViewModel:BindableObject
{
public ObservableCollection<Student> students { set; get; }
public static readonly BindableProperty FontSizeProperty =
BindableProperty.Create("fontsize", typeof(int), typeof(ColViewModel), null);
public int fontsize
{
get { return (int)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public ICommand IncreaseCommand { private set; get; }
public ColViewModel()
{students = new ObservableCollection<Student>();
getStudents();
fontsize = 24;
IncreaseCommand = new Command(() => {
fontsize ;
});
}
看法:
<StackLayout>
<Label Text="This is a Title" FontSize="{Binding fontsize}"/>
<CollectionView ItemsSource="{Binding students}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}" FontSize="{Binding Source={RelativeSource AncestorType={x:Type local:ColViewModel}}, Path=fontsize}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Click to increase fontsize" Command="{Binding IncreaseCommand}"/>
</StackLayout>
編輯:
xmlns:local="clr-namespace:MyForms2.ViewModels"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/491610.html
上一篇:嘗試計算字串中資料型別的數量
