C# 使用资源字典中定义的样式:
我在资源字典中为listboxitem定义了一个样式。我想在listbox的cs文件中使用此样式: 我正在做下面的事情,但是它给了我null,CustomListBoxItemStyle是一个给样式的键的名称C# 使用资源字典中定义的样式:,c#,silverlight,silverlight-4.0,C#,Silverlight,Silverlight 4.0,我在资源字典中为listboxitem定义了一个样式。我想在listbox的cs文件中使用此样式: 我正在做下面的事情,但是它给了我null,CustomListBoxItemStyle是一个给样式的键的名称 public class CustomListBox : ListBox { public CustomListBox() { this.ItemContainerStyle = Application.Current.Resources["Custom
public class CustomListBox : ListBox
{
public CustomListBox()
{
this.ItemContainerStyle = Application.Current.Resources["CustomListBoxItemStyle"] as Style;
}
}
没有用于此的xaml
如何做到这一点
我在资源字典中为listboxitem定义了一个样式
但是该资源字典是否合并到应用程序的资源字典中。它不会自动发生。您需要将其包含在App.xaml中,如下所示:-
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- other resource defined directly in app xaml -->
</ResourceDictionary>
</Application.Resources>
好的,我几乎尝试了所有的方法,但是没有办法摆脱
this.ItemContainerStyle=null
因此,我改变了操作方法,而是在从Listbox继承的customlistbox.cs中设置ItemContainerStyle。我从那里删除了它
然后,我使用的自定义listbox是一个usercontrol,因此它有axaml:),因此我在usercontrol.resource中定义了我的ItemContainerStyle,如下所示:
<UserControl.Resources>
<Style x:Key="CustomListBoxItemStyle" TargetType="ChargeEntry:CustomListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ChargeEntry:CustomListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid HorizontalAlignment="Stretch">
<TextBlock x:Name="txtName" />
</Grid>
<Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
<Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
看得见的
然后将其仅用于在usercontrol内部定义的自定义listbox
<CustomListBox x:Name="lstComboBoxResult" ItemContainerStyle="{StaticResource CustomListBoxItemStyle}" />
Stackoverflow使用的降价语言文档值得您快速阅读。当你写问题时,一个有用的大纲就在右边空白处。@AnthonyWJones我遵循了它,但它不起作用,它仍然产生空值。@Malcolm:嗯,你不得不“向我的项目添加一个应用程序xaml”这一事实表明事情已经有点混乱了。您应该已经有了App.xaml和App.xaml.cs,它的App.xaml.cs具有Application\u Startup
等功能。它是从应用程序派生的App
类,它的App
类InitializeComponent
方法将ResourceDictionary
分配给Application.Resources
属性。@Anthony mine项目silverlightClassLibrary项目,我再次创建了一个新的silverlightClassLibrary项目,但App.xaml似乎不存在,我必须手动添加..:(@Anthony,它似乎在那里)SilverlightWebApplication@Malcom:web应用程序没有,类库也没有,只有silverlight应用程序(实际创建XAP的应用程序)有App.xaml。使用该应用程序。类库中的资源可能不是一个好主意。