views:

47

answers:

2

I have a style defined for listboxitem in a resource dictionary. I want to use this style in a cs file of listbox :

I am doing the below thing but it gives me null ,CustomListBoxItemStyle is a name of a key given to the style.

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

There is no xaml for this.

How to achieve this?

A: 

I have a style defined for listboxitem in a resource dictionary.

but is that resource dictionary merged into the application's resource dictionary. It doesn't happen automatically you need to include it in the App.xaml like this:-

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- other resource defined directly in app xaml -->
    </ResourceDictionary>
</Application.Resources>
AnthonyWJones
ok so i have to add a app xaml to my project.
Malcolm
@AnthonyWJones i followed it but its not working , it still produces null.
Malcolm
@Malcolm: Umm the fact that you've had to "add a app xaml to my project" indicates that things are a bit messed up already. You should already have an App.xaml and an App.xaml.cs, its App.xaml.cs which has the `Application_Startup` etc. Its the `App` class which derives from the Application and its the `App` class `InitializeComponent` method which assigns the `ResourceDictionary` to the `Application.Resources` property.
AnthonyWJones
@Anthony mine project is silverlightClassLibrary project and I again created a new silverlightclasslibraryproject but the App.xaml doen't seems to be there, I have to add manually.. :(
Malcolm
@Anthony it does seems to be there in SilverlightWebApplication
Malcolm
@Malcom: The web application won't have one, nor will a class libary, only a silverlight application (the one actually creates a XAP) will have an App.xaml. Using the Application.Resources from with in a class library is probably not a good idea.
AnthonyWJones
A: 

Ok i tried almost every thing but there was no way to get rid of getting the this.ItemContainerStyle = null

So i changed the way of doing , Instead setting the ItemContainerStyle in a customlistbox.cs inherited from Listbox..I removed it from there.

Then where I was using mine custom listbox that was a usercontrol, so there was axaml for it :), hence i defined my ItemContainerStyle in the usercontrol.resource as shown below :

 <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>

and then used it to custom listbox defined inside the usercontrol only..

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