我需要為 UWP 應用程式動態更改 Picker 中每個專案的文本顏色。
比如現在Picker顯示的是Names串列,所以如果默認flag是0,我想把文字顏色改成紅色,如果Flag是1,把文字顏色改成綠色等等。有沒有辦法實作這個?請建議
uj5u.com熱心網友回復:
選擇器的 TextColor 屬性只能更改所選專案的文本顏色。如果要更改顯示項的文本顏色,可以嘗試使用自定義渲染器。
我創建了一個示例并更改了專案的文本顏色。xamarin 中的選取器是從 uwp 中的 ComboBox 繼承的控制元件。因此,您可以創建一個樣式來設定 ComboBox 的 Foreground 屬性。
如:在app.xaml(uwp專案部分)中:
<Application
x:Class="AppTest.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest.UWP">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="ComboBoxItem" x:Name="customtype">
<Setter Property="Foreground" Value="Blue"/>
</Style></ResourceDictionary>
</Application.Resources>
在customrender中:
[assembly: ExportRenderer(typeof(Picker),typeof(MyPickerRender))]
namespace AppTest.UWP
{
public class MyPickerRender : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Control.ItemContainerStyle = Windows.UI.Xaml.Application.Current.Resources["customtype"] as Windows.UI.Xaml.Style;
}
}
}
并且因為要根據標志的不同值設定不同的顏色,所以可以創建兩種樣式并根據標志使用不同的樣式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515956.html
