Materialdesigninxamltoolkit: ListBoxItem.ItemContainerStyle resets MaterialDesign style even with BasedOn property set

Created on 5 Oct 2016  路  12Comments  路  Source: MaterialDesignInXAML/MaterialDesignInXamlToolkit

Setting ListBox item style in the following way

<ListBox.ItemContainerStyle>
    <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="{x:Type ListBoxItem}">
        ...
    </Style>
</ListBox.ItemContainerStyle>

...resets the actual ListBoxItem style to default (not material design).

However, the same exact approach works fine for TreeView and TreeViewItem.

I fixed it by setting BasedOn property to "{StaticResource MaterialDesignListBoxItem}", but it's suboptimal because it relies on a key that may or may not change.

Perhaps I'm doing something wrong?

Most helpful comment

TargetType="{x:Type MaterialDesignListBoxItem}" should be "{x:Type ListBoxItem}" I think.
The original error is probably of the same cause as http://stackoverflow.com/a/7780980

All 12 comments

The library follows semver versioning rules, and style names are considered to be part of the public API, and therefore very highly unlikely to change.

...looks like the default is missing from .Defaults.xaml...however you can safely used the key name for now...it's not going to change.

Well ultimately it would be great if I didn't have any external dependencies in my XAML code, so that I could swap to a different theme (if need be) without any extra changes.

You can enter. But you can't leave :)

giphy

Wouldn't be my desire, but the project I'm working on isn't mine alone :(

I have the same problem. If if remove it gives error Window must be the root of the tree. Cannot add Window as a child of Visual.
But when I apply some style everything is working but with default listbox style.
<ListBox Grid.IsSharedSizeScope="True" x:Name="previewSongLyricsListbox" > <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <Label Grid.Row="0" Content="{Binding id}" Background="{Binding color}"/> <Label Grid.Row="1" Content="{Binding lyrics}" BorderThickness="0"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemContainerStyle> <Style BasedOn="{StaticResource MaterialDesignListBoxItem}" TargetType="{x:Type MaterialDesignListBoxItem}"> <Setter Property="Padding" Value="5"/> </Style> </ListBox.ItemContainerStyle> </ListBox>

TargetType="{x:Type MaterialDesignListBoxItem}" should be "{x:Type ListBoxItem}" I think.
The original error is probably of the same cause as http://stackoverflow.com/a/7780980

First, thanks for the fast answer! Yea, it works now but it is not material style. But when i try to remove ListBox.ItemContainerStyle, it gives the strange error i posted previous post.
http://imgur.com/a/plRZM

Did you change the style to

<Style BasedOn="{StaticResource MaterialDesignListBoxItem}" TargetType="{x:Type ListBoxItem}">

?

Because that worked for me.

It says that The resource "MaterialDesignListBoxItem" could not be resolved.

(I have xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" in <Window ... />)

It does say the same for me in design-time but works run-time

It gives the error when I set ItemSources to the listbox.
http://imgur.com/dmqXP5p
I am wondering is it possible to have ListBox with Data binding + material design, because the only way I can get the listbox with material design is to add items manually(listbox.Items.Add())

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheSylence picture TheSylence  路  12Comments

zirius666 picture zirius666  路  12Comments

rrs picture rrs  路  18Comments

StephanBis picture StephanBis  路  16Comments

darturdotnetdeveloper picture darturdotnetdeveloper  路  11Comments