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

时间:2010-05-20 10:03:17

标签: c# silverlight silverlight-4.0

我在资源字典中为listboxitem定义了一种样式。我想在listbox的cs文件中使用这种风格:

我正在做以下事情,但它给了我null,CustomListBoxItemStyle是给予样式的键的名称。

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

这没有xaml。

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

  

我在资源字典中为listboxitem定义了一种样式。

但该资源字典是否合并到应用程序的资源字典中。它不会自动发生,您需要将它包含在App.xaml中,如下所示: -

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- other resource defined directly in app xaml -->
    </ResourceDictionary>
</Application.Resources>

答案 1 :(得分:0)

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

所以我改变了做法,而是在从Listbox继承的customlistbox.cs中设置ItemContainerStyle。我从那里删除了它。

然后我在使用我的自定义列表框,这是一个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中定义的自定义列表框..

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