Z
Z
ZelibobA17062014-12-09 22:17:12
User interface
ZelibobA1706, 2014-12-09 22:17:12

What causes styles to disappear in WPF?

I wrote a custom style for the listview, connected it as a resourcedictionary, everything worked well. Then I set a couple of properties to the elements through listview.ItemContainerStyle and the styles set earlier disappear. What have I done wrong?

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>                    
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsChecked}"/>
            <Setter Property="IsEnabled" Value="{Binding IsEnabled}"></Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Papin, 2014-12-18
@ZelibobA1706

Once an ItemTemplate or ItemContainerStyle is overridden, the old styles are completely overwritten accordingly. Unspecified values ​​are overwritten with default values.
The declaration from the code example is equivalent to the code:

var view = new ListView();
view.ItemTemplate = new DataTemplate();
view.ItemContainerStyle = new Style() { ... };

If you need to change something in the style in the resource, you need to edit it exactly in the place where it is defined. If you need to have both styles, Expression Blend can make copies (right click on the control - Edit Template - Edit a copy). Well, or just copy and give a different resource key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question