C# 是否将XAML样式转换为代码隐藏样式?
我有一个列表框,它的选择颜色是默认的纯蓝色。我读了这篇文章“如何更改WPF列表框SelectedItem颜色?”。我想创建一种风格,让代码隐藏在其中。因此,我可以将此样式指定给我的Listbox ItemContainerStyle属性 像 风格s= MyListBox.ItemContainerStyle=s 我想在代码中隐藏这一点,因为如果用户更改了我的软件的主题,那么这个样式(选择颜色)应该重新创建以匹配更改的主题颜色C# 是否将XAML样式转换为代码隐藏样式?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个列表框,它的选择颜色是默认的纯蓝色。我读了这篇文章“如何更改WPF列表框SelectedItem颜色?”。我想创建一种风格,让代码隐藏在其中。因此,我可以将此样式指定给我的Listbox ItemContainerStyle属性 像 风格s= MyListBox.ItemContainerStyle=s 我想在代码中隐藏这一点,因为如果用户更改了我的软件的主题,那么这个样式(选择颜色)应该重新创建以匹配更改的主题颜色 <Style x:Key="SimpleListBoxItem"
<Style x:Key="SimpleListBoxItem" TargetType="ListBoxItem">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource AuthorGradient}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我认为您没有此代码的代码隐藏版本,您只需将现有模板应用到列表框中,如下所示 如果你的目标是一个模板
(NameOfListBox.SelectedItem as ListBoxItem).ContentTemplate = this.Resources["NameOfTemplate"] as DataTempate;
(NameOfListBox.SelectedItem as ListBoxItem).UpdateLayout();
如果你的目标是一种风格
(NameOfListBox.SelectedItem as ListBoxItem).Style= this.Resources["NameOfStyle"] as DataTempate;
(NameOfListBox.SelectedItem as ListBoxItem).UpdateLayout();
范例
(lstMetaDataCards.SelectedItem as ListBoxItem).ContentTemplate = this.Resources["MetaDataCardAtEditState"] as DataTemplate;
(lstMetaDataCards.SelectedItem as ListBoxItem).UpdateLayout();
我认为您没有此代码的代码隐藏版本,您只需将现有模板应用于列表框,如下所示 如果你的目标是一个模板
(NameOfListBox.SelectedItem as ListBoxItem).ContentTemplate = this.Resources["NameOfTemplate"] as DataTempate;
(NameOfListBox.SelectedItem as ListBoxItem).UpdateLayout();
如果你的目标是一种风格
(NameOfListBox.SelectedItem as ListBoxItem).Style= this.Resources["NameOfStyle"] as DataTempate;
(NameOfListBox.SelectedItem as ListBoxItem).UpdateLayout();
范例
(lstMetaDataCards.SelectedItem as ListBoxItem).ContentTemplate = this.Resources["MetaDataCardAtEditState"] as DataTemplate;
(lstMetaDataCards.SelectedItem as ListBoxItem).UpdateLayout();
你尝试过什么,遇到了什么问题?请编辑问题以添加此信息,包括您拥有的任何代码示例(即使它们已损坏)。@MerlynMorgan Graham:我已编辑了我的问题,请现在查看。这里有一个重复的问题:-从该示例开始,让Intellisense帮你完成剩下的工作。@MerlynMorgan Graham我已经调查过了你做了什么,遇到了什么问题?请编辑此问题以添加此信息,包括您拥有的任何代码示例(即使它们已损坏)。@MerlynMorgan Graham:我已编辑了我的问题,请立即查看。这里有一个重复的问题:-从该示例开始,让Intellisense帮助您完成其余内容。@MerlynMorgan Graham我已经查看过了