使用在资源字典中定义的样式:

3

我在资源字典中定义了一个列表框项的样式。我想在列表框的cs文件中使用这个样式:

我尝试了以下操作,但是它给了我一个null值,CustomListBoxItemStyle是样式的名称:

public class CustomListBox : ListBox
{  
    public CustomListBox()
    {
        this.ItemContainerStyle = Application.Current.Resources["CustomListBoxItemStyle"] as Style;
    }
}

这里没有XAML。

如何实现这个功能?


你可能值得快速阅读Stackoverflow使用的Markdown语言文档。当你编写问题时,右侧边栏上有一个有用的概要。 - AnthonyWJones
2个回答

1
我在资源字典中定义了一个列表框项的样式。
但是这个资源字典是否已经合并到应用程序的资源字典中呢?它不会自动发生,您需要像这样在 App.xaml 中包含它:
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- other resource defined directly in app xaml -->
    </ResourceDictionary>
</Application.Resources>

@AnthonyWJones 我按照你的方法做了,但是它还是不起作用,仍然返回 null。 - Malcolm
@Malcolm:嗯,你已经不得不“将一个app xaml添加到我的项目”这一事实表明事情已经有点混乱了。你应该已经有一个App.xaml和一个App.xaml.cs,它的App.xaml.cs有Application_Startup等内容。它是从Application派生的App类,而是App类的InitializeComponent方法将ResourceDictionary分配给Application.Resources属性。 - AnthonyWJones
@Anthony 我的项目是SilverlightClassLibrary项目,我又创建了一个新的SilverlightClassLibrary项目,但是App.xaml似乎不在那里,我必须手动添加.. :( - Malcolm
@Anthony,它似乎确实存在于SilverlightWebApplication中。 - Malcolm
@Malcom:Web应用程序不会有一个,类库也不会有,只有Silverlight应用程序(实际上创建XAP的应用程序)才会有App.xaml。从类库中使用Application.Resources可能不是一个好主意。 - AnthonyWJones

0

好的,我尝试了几乎所有的方法,但是没有办法摆脱 this.ItemContainerStyle = null

所以我改变了做事情的方式,而是在继承自Listbox的customlistbox.cs中设置ItemContainerStyle...我从那里将其删除。

然后,在我使用的自定义列表框(一个用户控件)中,有一个axaml:),因此我在用户控件资源中定义了我的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>

然后将其用于仅在用户控件内定义的自定义列表框中。

 <CustomListBox  x:Name="lstComboBoxResult"  ItemContainerStyle="{StaticResource CustomListBoxItemStyle}"   />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接