使用资源字典中定义的样式:
我在资源字典中为listboxitem定义了一种样式。我想在列表框的cs文件中使用这种样式:使用资源字典中定义的样式:
我正在做下面的事情,但它给了我null,CustomListBoxItemStyle是给予样式的关键字的名称。
public class CustomListBox : ListBox
{
public CustomListBox()
{
this.ItemContainerStyle = Application.Current.Resources["CustomListBoxItemStyle"] as Style;
}
}
这里没有xaml。
如何实现这一目标?
好吧,我几乎尝试了所有的事情,但没有办法摆脱得到this.ItemContainerStyle = null
这样的 - :你需要它包括在App.xaml中这样它不会自动发生我改变了做的方式,而是将ItemContainerStyle设置在从Listbox继承的customlistbox.cs中。我从那里删除它。
然后我在那里使用,这是一个用户控件,所以有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里面定义..
<CustomListBox x:Name="lstComboBoxResult" ItemContainerStyle="{StaticResource CustomListBoxItemStyle}" />
我在资源字典中为listboxitem定义了一种样式。
但是该资源字典合并到应用程序的资源字典中。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- other resource defined directly in app xaml -->
</ResourceDictionary>
</Application.Resources>
好吧,所以我必须添加一个应用程序xaml到我的项目。 – Malcolm 2010-05-20 10:42:04
@AnthonyWJones我跟着它,但它不工作,它仍然产生空。 – Malcolm 2010-05-20 10:58:05
@Malcolm:呃,您必须“将应用程序添加到我的项目中”这一事实表明事情已经有点混乱了。你应该已经有一个App.xaml和一个App.xaml.cs,它的App.xaml.cs有'Application_Startup'等。它的'App'类派生自Application和它的'App'类'InitializeComponent ''将'ResourceDictionary'分配给'Application.Resources'属性的方法。 – AnthonyWJones 2010-05-20 11:31:50
它可能是值得您快速阅读*使用的降价语言文档。当你写一个问题时,右边有一个有用的简介。 – AnthonyWJones 2010-05-20 10:26:43