[英]Visual Studio WPF XAML How to add different styles to multiple ComboBoxes?
我有2個ComboBox,我需要制作一個白色和一個黑色。
我右鍵單擊ComboBox>編輯模板> 編輯副本 。
創建新名稱或應用於所有人似乎沒有什么區別,因為它始終適用於所有人。 主模板中的LinearGradientBrush代碼始終會覆蓋所有ComboBox中的Style。 它們最終都具有相同的顏色。
<LinearGradientBrush x:Key="ComboBox.Static.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0F0" Offset="0.0"/>
<GradientStop Color="#FFE5E5E5" Offset="1.0"/>
</LinearGradientBrush>
我右鍵單擊ComboBox>編輯模板> 創建空 。
模板為空白。 我從Microsoft ComboBox模板示例代碼復制,並且返回錯誤。
<ControlTemplate x:Key="ComboBoxControlTemplateDark" TargetType="{x:Type ComboBox}">
<Grid/>
</ControlTemplate>
如果要更改背景顏色,請使用組合框的Background Color屬性。
如果要為不同的組合框添加自定義樣式,請在“資源分類”文件中定義樣式
在項目中添加類型新的文件,然后添加以下代碼
<Style x:Key="ComboboxRedStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Value="True">
<Setter Property="BackGround" value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ComboboxBlackStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Value="True">
<Setter Property="BackGround" value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
在app.xaml中添加以下代碼
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/projectName;component/Resources/DesignResourceDictionary.xaml"/>
<ResourceDictionary Source="/projectName;component/Resources/FocuseSettingResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
包含代碼后,您可以在需要的地方調用組合框樣式,如下面的代碼
<ComboBox Style="{StaticResource ComboboxRedStyle}"Margin="251,38,0,0" VerticalAlignment="Top" Width="250" FontWeight="Bold"/>
<ComboBox Style="{StaticResource ComboboxBlackStyle}"Margin="251,38,0,0" VerticalAlignment="Top" Width="250" FontWeight="Bold"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.