2017-05-29 78 views
0

我需要更改touggle按鈕的默認顏色。 我不能在XAML代碼中這樣做,因爲我在運行時從json文件接收顏色。UWP更改代碼中的切換按鈕的顏色

所以,我可以更改VB或C#代碼中的默認顏色? 不是Back或Fore顏色,而是開關顏色。

感謝

回答

1

所以,我可以改變VB或C#代碼的默認顏色?

Toggle​Button的開關顏色實際上是由視覺狀態改變的背景顏色。要更改其默認的顏色,你可以通過重新定義影響後面如下這個顏色代碼可視狀態使用的主題資源:

public MainPage() 
{ 
    this.InitializeComponent(); 
    Application.Current.Resources["ToggleButtonBackgroundChecked"] = new SolidColorBrush(Colors.Red); 
    Application.Current.Resources["ToggleButtonBackgroundCheckedPointerOver"] = new SolidColorBrush(Colors.Red); 
    Application.Current.Resources["ToggleButtonBackgroundCheckedPressed"] = new SolidColorBrush(Colors.Red); 
} 

XAML

<ToggleButton    
    Click="Button_Click" 
    Content="ToggleButton" /> 

更多細節請參考ToggleButton styles and templates

如果您正在使用Toggle​Switch你需要更新有以下兩種:

Application.Current.Resources["ToggleSwitchFillOnPointerOver"] = new SolidColorBrush(Colors.Red); 
Application.Current.Resources["ToggleSwitchFillOn"]= new SolidColorBrush(Colors.Red); 

儘量找到您所需要的樣式模板修改的資源。

+0

我曾試過但沒有改變 我必須寫任何其他行嗎? –

+0

@DanieleP。你使用了「ToggleButton」控件還是「ToggleSwitch」?什麼是你的uwp應用程序目標版本? –

+0

OPS我使用ToggleSwitch! –