ModernWpf icon indicating copy to clipboard operation
ModernWpf copied to clipboard

Adding Style Property will cause either fallback to default windows style or binding issues

Open johanneszellinger opened this issue 3 years ago • 12 comments

I have a ListView with a GridView containing a templated columns. I want to add the following style property to my listview:

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem" >
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                    </Style>
                </ListView.ItemContainerStyle>

This works, however the base style is now overriden and the appearance is as follows: image

To keep the modern style, I added the BasedOn Tag:

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem" BasedOn="{ui:StaticResource DefaultListViewItemStyle}">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                    </Style>
                </ListView.ItemContainerStyle>

Now, the modern style is back, but data-binding to the DisplayMembers won't work anymore: image

Is this a mistake on my end, or an issue of the framework?

johanneszellinger avatar Apr 26 '21 07:04 johanneszellinger

Look in output panel to see if there is error or not

ghost1372 avatar Apr 26 '21 07:04 ghost1372

@ghost1372 I have no errors (databinding or other) in the output on both versions of the code above.

johanneszellinger avatar Apr 26 '21 07:04 johanneszellinger

Can you upload a sample project?

ghost1372 avatar Apr 26 '21 07:04 ghost1372

I have thrown together a little dummy project, you can clone here: https://github.com/TheRealRolandDeschain/ModernWPFListviewIssue

(I made a lightgreen background due to the white text color, but that shouldn't distract from the issue above)

johanneszellinger avatar Apr 26 '21 08:04 johanneszellinger

Works without problems, I noticed that you do not set IsSelected value in ControllableLedModel and ControllableLedList.Add

public ControllableLedModel(bool isselected, double _positionX, double _positionY, uint _luminosity = 0)
        {
            PositionX = _positionX;
            PositionY = _positionY;
            Luminosity = _luminosity;
            IsSelected = isselected;
        }

and ControllableLedList.Add(new ControllableLedModel(true,i * 1.5, i * 2.5, (uint)(i * 10))); image

ghost1372 avatar Apr 26 '21 09:04 ghost1372

@ghost1372 The binding to IsSelected wasn't the problem (in my actual project it is set). The problem is, that the different columns are not displayed correctly. This can be seen in your screenshot as well. If the added style property is commented out, we can see the property values of the bound ControllableLedModel instances in the respective columns of the ListView As soon as I add the style property, you can only see the string "ModernWPFListviewIssue.ControllableLedModel".

johanneszellinger avatar Apr 26 '21 09:04 johanneszellinger

oh you right, remove itemcontainerStyle and use this code


<ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                </Style>
            </ListView.Resources>

image

ghost1372 avatar Apr 26 '21 10:04 ghost1372

Oh, I'm really sorry to drag this issue out so long, but this doesn't work as well: Changes to IsSelected don't have any effect on the ListView Selection and the same vice versa with this code.

To add a bit of context: In my actual project I have two of those Listviews, where if one of them has Items selected, I want to have the selection mirrored on the second one automatically. This is why I need the selection bound to the items....

johanneszellinger avatar Apr 26 '21 10:04 johanneszellinger

Here is a screenshot of the actual project: listviews

The left is a ListView on a canvas to represent a schematic overview of the actual hardware I want to control with this GUI. The right is the ListView from the dummy project. If the user selects a item on the left, I want to automatically have them selected in the right list as well (and vice versa).

johanneszellinger avatar Apr 26 '21 10:04 johanneszellinger

Any update on this issue?

johanneszellinger avatar May 05 '21 10:05 johanneszellinger

I am having a similar issue. I am trying to center the ListView items, but I am getting the same problem as above. Here is my code:

<ListView.ItemContainerStyle>
	<Style TargetType="ListViewItem" BasedOn="{StaticResource DefaultListViewItemStyle}">
		<Setter Property="VerticalContentAlignment" Value="Center" />
	</Style>
</ListView.ItemContainerStyle>

feelgood-interface avatar Aug 04 '22 13:08 feelgood-interface

I have this problem too. Any updates?

Axeedd avatar Oct 02 '23 11:10 Axeedd